Mercurial > p > roundup > code
annotate roundup/dist/command/build_scripts.py @ 5525:bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
The roundup-admin export and import commands are used for migrating
between different database backends. It is desirable that they should
be usable also for migrations between Python 2 and Python 3, and in
some cases (e.g. with the anydbm backend) this may be required.
To be usable for such migrations, the format of the generated CSV
files needs to be stable, meaning the same as currently used with
Python 2. The export process uses repr() to produce the fields in the
CSV files and eval() to convert them back to Python data structures.
repr() of strings with non-ASCII characters produces different results
for Python 2 and Python 3.
This patch adds repr_export and eval_import functions to
roundup/anypy/strings.py which provide the required operations that
are just repr() and eval() in Python 2, but are more complicated in
Python 3 to use data representations compatible with Python 2. These
functions are then used in the required places for export and import.
repr() and eval() are also used in storing the dict of changed values
in the journal for the RDBMS backends. It is similarly desirable that
the database be compatible between Python 2 and Python 3, so that
export and import do not need to be used for a migration between
Python versions for non-anydbm back ends. Thus, this patch changes
rdbms_common.py in the places involved in storing journals in the
database, not just in those involved in import/export.
Given this patch, import/export with non-ASCII characters appear based
on some limited testing to work across Python versions, and an
instance using the sqlite backend appears to be compatible between
Python versions without needing import/export, *if* the sessions/otks
databases (which use anydbm) are deleted when changing Python version.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 02 Sep 2018 23:48:04 +0000 |
| parents | 12baa5b9b597 |
| children |
| rev | line source |
|---|---|
| 4068 | 1 # |
| 2 # Copyright (C) 2009 Stefan Seefeld | |
| 3 # All rights reserved. | |
| 4 # For license terms see the file COPYING.txt. | |
| 5 # | |
| 6 from distutils.command.build_scripts import build_scripts as base | |
|
5023
5e2888db6c48
build_scripts: self.announce --> log.info because it is visible
anatoly techtonik <techtonik@gmail.com>
parents:
5022
diff
changeset
|
7 from distutils import log |
| 4068 | 8 import sys, os, string |
| 9 | |
| 10 class build_scripts(base): | |
| 11 """ Overload the build_scripts command and create the scripts | |
| 12 from scratch, depending on the target platform. | |
| 13 | |
| 14 You have to define the name of your package in an inherited | |
| 15 class (due to the delayed instantiation of command classes | |
| 16 in distutils, this cannot be passed to __init__). | |
| 17 | |
| 18 The scripts are created in an uniform scheme: they start the | |
| 19 run() function in the module | |
| 20 | |
| 21 <packagename>.scripts.<mangled_scriptname> | |
| 22 | |
| 23 The mangling of script names replaces '-' and '/' characters | |
| 24 with '-' and '.', so that they are valid module paths. | |
| 25 | |
| 26 If the target platform is win32, create .bat files instead of | |
| 27 *nix shell scripts. Target platform is set to "win32" if main | |
| 28 command is 'bdist_wininst' or if the command is 'bdist' and | |
| 29 it has the list of formats (from command line or config file) | |
| 30 and the first item on that list is wininst. Otherwise | |
| 31 target platform is set to current (build) platform. | |
| 32 """ | |
| 33 package_name = 'roundup' | |
| 34 | |
| 35 def initialize_options(self): | |
| 36 base.initialize_options(self) | |
| 37 self.script_preamble = None | |
| 38 self.target_platform = None | |
| 39 self.python_executable = None | |
| 40 | |
| 41 def finalize_options(self): | |
| 42 base.finalize_options(self) | |
| 43 cmdopt=self.distribution.command_options | |
| 44 | |
| 45 # find the target platform | |
| 46 if self.target_platform: | |
| 47 # TODO? allow explicit setting from command line | |
| 48 target = self.target_platform | |
|
5381
0942fe89e82e
Python 3 preparation: change "x.has_key(y)" to "y in x".
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
49 if "bdist_wininst" in cmdopt: |
| 4068 | 50 target = "win32" |
|
5381
0942fe89e82e
Python 3 preparation: change "x.has_key(y)" to "y in x".
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
51 elif "formats" in cmdopt.get("bdist", {}): |
| 4068 | 52 formats = cmdopt["bdist"]["formats"][1].split(",") |
| 53 if formats[0] == "wininst": | |
| 54 target = "win32" | |
| 55 else: | |
| 56 target = sys.platform | |
| 57 if len(formats) > 1: | |
| 58 self.warn( | |
| 59 "Scripts are built for %s only (requested formats: %s)" | |
| 60 % (target, ",".join(formats))) | |
| 61 else: | |
| 62 # default to current platform | |
| 63 target = sys.platform | |
|
5022
9250620c7219
build_scripts: Fix long term bug with setting self.target_platform
anatoly techtonik <techtonik@gmail.com>
parents:
4068
diff
changeset
|
64 self.target_platform = target |
| 4068 | 65 |
| 66 # for native builds, use current python executable path; | |
| 67 # for cross-platform builds, use default executable name | |
| 68 if self.python_executable: | |
| 69 # TODO? allow command-line option | |
| 70 pass | |
| 71 if target == sys.platform: | |
| 72 self.python_executable = os.path.normpath(sys.executable) | |
| 73 else: | |
| 74 self.python_executable = "python" | |
| 75 | |
| 76 # for windows builds, add ".bat" extension | |
| 77 if target == "win32": | |
| 78 # *nix-like scripts may be useful also on win32 (cygwin) | |
| 79 # to build both script versions, use: | |
| 80 #self.scripts = list(self.scripts) + [script + ".bat" | |
| 81 # for script in self.scripts] | |
| 82 self.scripts = [script + ".bat" for script in self.scripts] | |
| 83 | |
| 84 # tweak python path for installations outside main python library | |
|
5381
0942fe89e82e
Python 3 preparation: change "x.has_key(y)" to "y in x".
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
85 if "prefix" in cmdopt.get("install", {}): |
| 4068 | 86 prefix = os.path.expanduser(cmdopt['install']['prefix'][1]) |
| 87 version = '%d.%d'%sys.version_info[:2] | |
| 88 self.script_preamble = """ | |
| 89 import sys | |
| 90 sys.path.insert(1, "%s/lib/python%s/site-packages") | |
| 91 """%(prefix, version) | |
| 92 else: | |
| 93 self.script_preamble = '' | |
| 94 | |
| 95 def copy_scripts(self): | |
| 96 """ Create each script listed in 'self.scripts' | |
| 97 """ | |
| 98 | |
|
5435
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
99 try: |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
100 # Python 3. |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
101 maketrans = str.maketrans |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
102 except AttributeError: |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
103 # Python 2. |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
104 maketrans = string.maketrans |
|
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
105 to_module = maketrans('-/', '_.') |
| 4068 | 106 |
| 107 self.mkpath(self.build_dir) | |
| 108 for script in self.scripts: | |
| 109 outfile = os.path.join(self.build_dir, os.path.basename(script)) | |
| 110 | |
| 111 #if not self.force and not newer(script, outfile): | |
| 112 # self.announce("not copying %s (up-to-date)" % script) | |
| 113 # continue | |
| 114 | |
| 115 if self.dry_run: | |
|
5023
5e2888db6c48
build_scripts: self.announce --> log.info because it is visible
anatoly techtonik <techtonik@gmail.com>
parents:
5022
diff
changeset
|
116 log.info("would create %s" % outfile) |
| 4068 | 117 continue |
| 118 | |
| 119 module = os.path.splitext(os.path.basename(script))[0] | |
|
5435
12baa5b9b597
Python 3 preparation: avoid string.translate() and string.maketrans().
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
120 module = module.translate(to_module) |
| 4068 | 121 script_vars = { |
| 122 'python': self.python_executable, | |
| 123 'package': self.package_name, | |
| 124 'module': module, | |
| 125 'prefix': self.script_preamble, | |
| 126 } | |
| 127 | |
|
5023
5e2888db6c48
build_scripts: self.announce --> log.info because it is visible
anatoly techtonik <techtonik@gmail.com>
parents:
5022
diff
changeset
|
128 log.info("writing %s" % outfile) |
| 4068 | 129 file = open(outfile, 'w') |
| 130 | |
| 131 try: | |
| 132 # could just check self.target_platform, | |
| 133 # but looking at the script extension | |
| 134 # makes it possible to build both *nix-like | |
| 135 # and windows-like scripts on win32. | |
| 136 # may be useful for cygwin. | |
| 137 if os.path.splitext(outfile)[1] == ".bat": | |
| 138 file.write('@echo off\n' | |
| 139 'if NOT "%%_4ver%%" == "" "%(python)s" -c "from %(package)s.scripts.%(module)s import run; run()" %%$\n' | |
| 140 'if "%%_4ver%%" == "" "%(python)s" -c "from %(package)s.scripts.%(module)s import run; run()" %%*\n' | |
| 141 % script_vars) | |
| 142 else: | |
| 143 file.write('#! %(python)s\n%(prefix)s' | |
| 144 'from %(package)s.scripts.%(module)s import run\n' | |
| 145 'run()\n' | |
| 146 % script_vars) | |
| 147 finally: | |
| 148 file.close() | |
|
5380
64c4e43fbb84
Python 3 preparation: numeric literal syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5023
diff
changeset
|
149 os.chmod(outfile, 0o755) |
