Skip to content

Commit

Permalink
Add module renaming to install_lib command
Browse files Browse the repository at this point in the history
  • Loading branch information
Legrandin committed Mar 7, 2016
1 parent 4409aa4 commit 018d2a2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 39 deletions.
10 changes: 10 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

3.4.2 (8 March 2016)
+++++++++++++++++++


Resolved issues
---------------

* Fix renaming of package for ``install`` command.


3.4.1 (21 February 2016)
+++++++++++++++++++

Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ All the code can be downloaded from `GitHub`_.
News
----

* **21 Feb 2016 (NEW)**. Release 3.4.1.
* **8 Mar 2016 (NEW)**. Bugfix release 3.4.2.
* 21 Feb 2016. Release 3.4.1.
* 7 Feb 2016. Release 3.4.
* Nov 2015. Release 3.3.1.
* 29 Oct 2015. Release 3.3.
Expand Down
2 changes: 1 addition & 1 deletion lib/Crypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
__all__ = ['Cipher', 'Hash', 'Protocol', 'PublicKey', 'Util', 'Signature',
'IO', 'Math']

version_info = (3, 4, 1)
version_info = (3, 4, 2)
108 changes: 71 additions & 37 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
from distutils.core import Extension, Command, setup
from distutils.command.build_ext import build_ext
from distutils.command.build import build
from distutils.command.install_lib import install_lib
from distutils.errors import CCompilerError
import distutils
import os, sys, shutil
import re, os, sys, shutil

use_separate_namespace = os.path.isfile(".separate_namespace")

Expand Down Expand Up @@ -134,6 +135,7 @@ def PrintErr(*args, **kwd):
w(str(a))
w(kwd.get("end", "\n"))


def test_compilation(program, extra_cc_options=None, extra_libraries=None):
"""Test if a certain C program can be compiled."""

Expand Down Expand Up @@ -189,11 +191,75 @@ def test_compilation(program, extra_cc_options=None, extra_libraries=None):

return result

def change_module_name(file_name):
"""Change any occurrance of 'Crypto' to 'Cryptodome'."""

fd = open(file_name, "rt")
content = (fd.read().
replace("Crypto.", "Cryptodome.").
replace("Crypto ", "Cryptodome ").
replace("'Crypto'", "'Cryptodome'").
replace('"Crypto"', '"Cryptodome"'))
fd.close()

os.remove(file_name)

fd = open(file_name, "wt")
fd.write(content)
fd.close()


def rename_crypto_dir(build_lib):
"""Move all files from the 'Crypto' package to the
'Cryptodome' package in the given build directory"""

source = os.path.join(build_lib, "Crypto")
target = os.path.join(build_lib, "Cryptodome")

if not os.path.exists(target):
PrintErr("Creating directory %s" % target)
os.makedirs(target)
else:
PrintErr("Directory %s already exists" % target)

# Crypto package becomes Cryptodome
for root_src, dirs, files in os.walk(source):

root_dst, nr_repl = re.subn('Crypto', 'Cryptodome', root_src)
assert nr_repl == 1

for dir_name in dirs:
full_dir_name_dst = os.path.join(root_dst, dir_name)
if not os.path.exists(full_dir_name_dst):
os.makedirs(full_dir_name_dst)

for file_name in files:
full_file_name_src = os.path.join(root_src, file_name)
full_file_name_dst = os.path.join(root_dst, file_name)

PrintErr("Copying file %s to %s" % (full_file_name_src, full_file_name_dst))
shutil.copy2(full_file_name_src, full_file_name_dst)

if file_name.endswith(".py"):
change_module_name(full_file_name_dst)



class PCTBuildExt (build_ext):

aesni_mod_names = "Crypto.Cipher._raw_aesni",

def run(self):
build_ext.run(self)

if use_separate_namespace:
rename_crypto_dir(self.build_lib)

# Clean-up (extensions are built last)
crypto_dir = os.path.join(self.build_lib, "Crypto")
PrintErr("Deleting directory %s" % crypto_dir)
shutil.rmtree(crypto_dir)

def build_extensions(self):
# Disable any assembly in libtomcrypt files
self.compiler.define_macro("LTC_NO_ASM")
Expand Down Expand Up @@ -274,6 +340,7 @@ def remove_extensions(self, names):

self.extensions = [ x for x in self.extensions if x.name not in names ]


class PCTBuildPy(build_py):
def find_package_modules(self, package, package_dir, *args, **kwargs):
modules = build_py.find_package_modules(self, package, package_dir,
Expand All @@ -286,43 +353,11 @@ def find_package_modules(self, package, package_dir, *args, **kwargs):
retval.append(item)
return retval

class PCTBuild(build):

def run(self):
build.run(self)

if not use_separate_namespace:
return

PrintErr("Renaming Crypto to Cryptodome...\n")

# Rename root package
source = os.path.join(self.build_lib, "Crypto")
target = os.path.join(self.build_lib, "Cryptodome")
try:
shutil.rmtree(target)
except OSError:
pass
os.rename(source, target)

# Crypto becomes Cryptodome
for roots, dirs, files in os.walk(target):
files = [os.path.join(roots, f) for f in files if f.endswith(".py")]
for filepy in files:

fd = open(filepy, "rt")
content = (fd.read().
replace("Crypto.", "Cryptodome.").
replace("Crypto ", "Cryptodome ").
replace("'Crypto'", "'Cryptodome'").
replace('"Crypto"', '"Cryptodome"'))
fd.close()

os.remove(filepy)
build_py.run(self)
if use_separate_namespace:
rename_crypto_dir(self.build_lib)

fd = open(filepy, "wt")
fd.write(content)
fd.close()

class TestCommand(Command):

Expand Down Expand Up @@ -462,7 +497,6 @@ def run(self):
"Crypto.Math" : [ "mpir.dll" ],
},
cmdclass = {
'build':PCTBuild,
'build_ext':PCTBuildExt,
'build_py': PCTBuildPy,
'test': TestCommand
Expand Down

0 comments on commit 018d2a2

Please sign in to comment.