-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
53 lines (40 loc) · 1.54 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
#########################################
import os, sys, inspect
def script_abspath(frame=inspect.currentframe()):
p = os.path.split(inspect.getfile(frame))[0]
absdir = os.path.realpath(os.path.abspath(p))
return absdir
def script_abspath_parent(frame=inspect.currentframe()):
return os.path.dirname(script_abspath(frame))
def include_dir(subdir=None, frame=inspect.currentframe()):
# NOTES:
# DO NOT USE __file__ !!!
# dir = os.path.dirname(os.path.abspath(__file__))
# __file__ fails if script is called in different ways on Windows
# __file__ fails if someone does os.chdir() before
# sys.argv[0] also fails because it doesn't not always contains
# the path
#
# realpath() will make your script run, even if you symlink it
p = os.path.split(inspect.getfile(frame))[0]
incdir = os.path.realpath(os.path.abspath(p))
if incdir not in sys.path:
sys.path.insert(0, incdir)
if subdir:
# use this if you want to include modules from a subfolder
incdir = os.path.realpath(os.path.abspath(os.path.join(p, subdir)))
if incdir not in sys.path:
sys.path.insert(0, incdir)
###########################################################
# include dir and parent dirs
absdir = script_abspath()
while os.path.isdir(absdir):
pkgini = os.path.join(absdir, '__init__.py')
if not os.path.exists(pkgini):
break
if os.path.isdir(pkgini):
break
include_dir(absdir)
absdir = os.path.dirname(absdir)