-
Notifications
You must be signed in to change notification settings - Fork 66
/
update_cgat.py
187 lines (141 loc) · 5.9 KB
/
update_cgat.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
################################################################################
#
# MRC FGU Computational Genomics Group
#
# $Id: script_template.py 2871 2010-03-03 10:20:44Z andreas $
#
# Copyright (C) 2009 Andreas Heger
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#################################################################################
'''
update_cgat.py - change CGAT scripts to new package layout
==========================================================
:Author:
:Release: $Id$
:Date: |today|
:Tags: Python
Purpose
-------
scripts to update CGAT files such that they conform to
new package layout.
The script works by building a collection of all CGAT modules
and will replace import statements in all python scripts in
a directory. For example::
import IOTools
import Experiment as E
import GTF, GFF
will become::
import CGAT.IOTools as IOTools
import CGAT.Experiment as E
import CGAT.GTF as GTF
import CGAT.GFF as GFF
The script will create ".bak" files for each python script
it processes (even if they are not changed).
The script is not recursive, if you have python scripts in subdirectories,
please execute the script on each directory individually. To automate this,
you can use the ``find`` command.
Usage
-----
To update all python scripts in the current directory, type::
python update_cgat.py -d .
Type::
python update_cgat.py --help
for command line help.
Documentation
-------------
Code
----
'''
import os
import sys
import re
import optparse
import glob
import shutil
import CGAT.Experiment as E
def main( argv = None ):
"""script main.
parses command line options in sys.argv, unless *argv* is given.
"""
if not argv: argv = sys.argv
# setup command line parser
parser = optparse.OptionParser( version = "%prog version: $Id: script_template.py 2871 2010-03-03 10:20:44Z andreas $",
usage = globals()["__doc__"] )
parser.add_option("-d", "--directory", dest="directory", type="string",
help="supply help" )
parser.set_defaults( directory = ".",
# using Andreas' repository in order to delay
# changes to main repository in /ifs/devel/cgat
basename = "/ifs/devel/andreas/cgat/",
)
## add common options (-h/--help, ...) and parse command line
(options, args) = E.Start( parser, argv = argv )
# collect a list of python modules
module_names = [ os.path.basename(x)[:-3] for x in glob.glob(options.basename + "CGAT/*.py" )]
pipeline_names = [ os.path.basename(x)[:-3] for x in glob.glob(options.basename + "CGATPipelines/Pipeline*.py" )]
if options.directory == "CGAT":
files_to_update = glob.glob( os.path.join( basename, "scripts/*.py" )) +\
glob.glob( os.path.join( options.basename, "scripts/*.pyx" )) +\
glob.glob( os.path.join( options.basename, "CGATPipelines/pipeline_*.py" ) )+\
glob.glob( os.path.join( options.basename, "CGATPipelines/Pipeline*.py" ) )
else:
files_to_update = glob.glob( os.path.join( options.directory, "*.py" ))
E.info("updating %i python scripts/modules" % len(files_to_update))
counter = E.Counter()
for script in files_to_update:
counter.input += 1
print "working on", script
inf = open( script )
lines = inf.readlines()
inf.close()
# create a backup copy
shutil.move( script, script + ".bak" )
outf = open( script, "w" )
updated = False
for line in lines:
if re.match( "import ", line ):
if " as " in line:
try:
module, name = re.match( "import (\S+) as (\S+)\s*$", line).groups()
except AttributeError as msg:
raise AttributeError( "parsing error in line '%s': '%s'" % (line[:-1], msg))
if module in module_names:
line = "import CGAT.%s as %s\n" % (module, name )
updated = True
else:
try:
modules = re.match( "import (.+)", line ).groups()[0]
except AttributeError as msg:
raise AttributeError( "parsing error in line '%s': '%s'" % (line[:-1], msg))
modules = [ x.strip() for x in modules.split(",")]
for module in modules:
if module in module_names:
outf.write( "import CGAT.%s as %s\n" % (module, module ))
updated = True
elif module in pipeline_names:
outf.write( "import CGATPipelines.%s as %s\n" % (module, module ))
updated = True
else:
outf.write( "import %s\n" % module )
continue
outf.write( line )
outf.close()
if updated: counter.updated += 1
E.info( "summary: %s" % str(counter))
## write footer and output benchmark information.
E.Stop()
if __name__ == "__main__":
sys.exit( main( sys.argv) )