Skip to content

Commit

Permalink
remove manager lock and add py3 compatibility, version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yodeng committed Jul 21, 2022
1 parent 9d6d92c commit 421b084
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#### 依赖

+ Linux
+ Python >=2.7.10, <3
+ Python >=2.7.10, <=3.10
+ cython


Expand All @@ -27,7 +27,7 @@ pip install git+https://github.com/yodeng/ccbuild.git
> Pypi
```
pip install ccbuild
pip install -U ccbuild
```


Expand Down
6 changes: 3 additions & 3 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package:
build:
number: 0
skip: True # [not linux64]
string: py27
string: py
script:
- {{ PYTHON }} -m pip install -vv .

Expand All @@ -16,11 +16,11 @@ source:

requirements:
host:
- python <3
- python >=2.7,<3.11
- pip
- cython
run:
- python <3
- python >=2.7,<3.11
- pip
- cython
about:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def getdes():
version=get_version(),
packages=[PKG],
license="MIT",
#package_dir = {PKG:"src"},
#package_dir={PKG: "src"},
install_requires=["setuptools", "cython"],
python_requires='>=2.7, <3',
python_requires='>=2.7, <=3.10',
ext_modules=getExtension(),
long_description=getdes(),
long_description_content_type='text/markdown',
Expand Down
57 changes: 38 additions & 19 deletions src/_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def list_compile_files(self):
elif os.path.isdir(self.pdir):
mkdir(self.cdir)
cpcmd = "cp -r %s/* %s" % (self.pdir, self.cdir)
call(cpcmd)
self.call(cpcmd)
for p, ds, fs in os.walk(self.cdir):
dn = os.path.basename(p)
for dp in self.edir:
Expand All @@ -53,19 +53,19 @@ def compile(self, pyf):
self.logger.info("start compile %s file", pyf)
fin = False
with tempdir() as td:
with self.lock:
self.tmdir.append(td)
tf = os.path.join(td, "ccbuild.py")
tf = os.path.join(td, "ccbuild_%s" % getGID())
self.write_setup(tf)
cmd = [self.interpreter, tf, pyf]
_so = call(cmd, out=True, shell=False,
msg=pyf, c=self.cc, tmdir=td)
_so = self.call(cmd, out=True, shell=False,
msg=pyf, c=self.cc, tmdir=td)
if _so:
new_file_name = pyf[:-3] + ".so"
shutil.move(_so, new_file_name)
if os.path.isfile(new_file_name):
os.remove(pyf)
fin = True
if os.path.isfile(pyf[:-3]+".c"):
os.remove(pyf[:-3]+".c")
if fin:
self.logger.info("finished %s", pyf)

Expand All @@ -75,21 +75,40 @@ def __call__(self, pyfile):
def compile_all(self):
if len(self.compile_file) == 0:
self.list_compile_files()
mg = mp.Manager()
self.lock = mg.Lock()
self.tmdir = mg.list()
p = mp.Pool(self.threads)
p.map(self, self.compile_file)
self.clean_tmp()
self.clean_source()

def call(self, cmd, out=False, shell=True, msg="", c=False, tmdir=None):
if not out:
with open(os.devnull, "w") as fo:
subprocess.check_call(cmd, shell=shell, stdout=fo, stderr=fo)
return
try:
p.map(self, self.compile_file)
except WorkerStopException:
p.close()
for d in self.tmdir:
if os.path.isdir(d):
shutil.rmtree(d)
mg.shutdown()
self.clean_source()
clean_process()
return
out = subprocess.check_output(
cmd, shell=shell, stderr=subprocess.PIPE)
except Exception:
if tmdir and os.path.isdir(tmdir):
shutil.rmtree(tmdir)
if msg:
self.logger.error("compile error %s" % msg)
if not c:
self.safe_exit_when_error()
return
dotso = re.findall(" \-o (.+\.so)\n", out.decode())
return dotso[0]

def safe_exit_when_error(self):
self.clean_tmp()
self.clean_source()
clean_process()

@staticmethod
def clean_tmp():
for c in glob.glob("/tmp/*/ccbuild_%s" % getGID()):
if os.path.isdir(os.path.dirname(c)):
shutil.rmtree(os.path.dirname(c))

def clean_source(self):
for p, ds, fs in os.walk(self.cdir):
Expand Down
2 changes: 1 addition & 1 deletion src/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0"
__version__ = "1.1.0"
1 change: 0 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def main():
len(cp.compile_file), args.python)
if len(cp.compile_file) > 0:
cp.compile_all()
cp.clean_source()
else:
sys.exit(1)

Expand Down
29 changes: 6 additions & 23 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ class WorkerStopException(Exception):
pass


def clean_process():
def getGID():
p = os.getpid()
g = os.getpgid(p)
os.killpg(g, 15)
return os.getpgid(p)


def clean_process(ret_code=15):
os.killpg(getGID(), ret_code)


def pickle_method(method):
Expand Down Expand Up @@ -74,26 +77,6 @@ def tempdir(*args, **kwargs):
pass


def call(cmd, out=False, shell=True, msg="", c=False, tmdir=None):
log = mp.get_logger()
if not out:
with open(os.devnull, "w") as fo:
subprocess.check_call(cmd, shell=shell, stdout=fo, stderr=fo)
return
try:
out = subprocess.check_output(cmd, shell=shell, stderr=subprocess.PIPE)
except Exception as err:
if msg:
log.error("compile error %s" % msg)
if not c:
if tmdir and os.path.isdir(tmdir):
shutil.rmtree(tmdir)
raise WorkerStopException()
return
dotso = re.findall(" \-o (.+\.so)\n", out.decode())
return dotso[0]


def mkdir(path):
if not os.path.isdir(path):
os.makedirs(path)
Expand Down

0 comments on commit 421b084

Please sign in to comment.