diff options
author | maruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-27 20:56:51 +0000 |
---|---|---|
committer | maruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-27 20:56:51 +0000 |
commit | 2fac37585d3dfa0d7cf7a976698aae7627186573 (patch) | |
tree | d2f233a6afa5d8093f15f2f3c56cc4c009b05904 | |
parent | 071302929ab813f647e51253af4e885b33eab463 (diff) | |
download | chromium_src-2fac37585d3dfa0d7cf7a976698aae7627186573.zip chromium_src-2fac37585d3dfa0d7cf7a976698aae7627186573.tar.gz chromium_src-2fac37585d3dfa0d7cf7a976698aae7627186573.tar.bz2 |
Fix python scripts in src/chrome/
Make sure that:
- shebang is only present for executable files
- shebang is #!/usr/bin/env python
- __main__ is only present for executable files
- file's executable bit is coherent
Also fix EOF LF to be only one.
Minor python style fixes.
TBR=nirnimesh@chromium.org
BUG=105108
TEST=
Review URL: http://codereview.chromium.org/8680018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111658 0039d316-1c4b-4281-b951-d872f2087c98
69 files changed, 598 insertions, 626 deletions
diff --git a/chrome/PRESUBMIT.py b/chrome/PRESUBMIT.py index e9f377b..087232f 100755..100644 --- a/chrome/PRESUBMIT.py +++ b/chrome/PRESUBMIT.py @@ -1,4 +1,4 @@ -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/app/policy/syntax_check_policy_template_json.py b/chrome/app/policy/syntax_check_policy_template_json.py index 8c597be..8ef76fe 100644..100755 --- a/chrome/app/policy/syntax_check_policy_template_json.py +++ b/chrome/app/policy/syntax_check_policy_template_json.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -413,5 +413,4 @@ class PolicyTemplateChecker(object): if __name__ == '__main__': - checker = PolicyTemplateChecker() - sys.exit(checker.Run(sys.argv)) + sys.exit(PolicyTemplateChecker().Run(sys.argv)) diff --git a/chrome/browser/chromeos/PRESUBMIT.py b/chrome/browser/chromeos/PRESUBMIT.py index 430c0f6..77a9310 100755..100644 --- a/chrome/browser/chromeos/PRESUBMIT.py +++ b/chrome/browser/chromeos/PRESUBMIT.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/browser/chromeos/input_method/gen_ibus_input_methods.py b/chrome/browser/chromeos/input_method/gen_ibus_input_methods.py index fe98658..2de02c2 100644..100755 --- a/chrome/browser/chromeos/input_method/gen_ibus_input_methods.py +++ b/chrome/browser/chromeos/input_method/gen_ibus_input_methods.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/browser/chromeos/input_method/litify_proto_file.py b/chrome/browser/chromeos/input_method/litify_proto_file.py index 6d74667..c62d4f6 100644..100755 --- a/chrome/browser/chromeos/input_method/litify_proto_file.py +++ b/chrome/browser/chromeos/input_method/litify_proto_file.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -11,22 +11,23 @@ to the input .proto file. Run it like: litify_proto_file.py input.proto output.proto - """ import fileinput import sys + def main(argv): if len(argv) != 3: print 'Usage: litify_proto_file.py [input] [output]' - sys.exit(1) + return 1 output_file = open(sys.argv[2], 'w') for line in fileinput.input(sys.argv[1]): output_file.write(line) output_file.write("\noption optimize_for = LITE_RUNTIME;\n") + return 0 if __name__ == '__main__': - main(sys.argv) + sys.exit(main(sys.argv)) diff --git a/chrome/browser/resources/file_manager/bin/squashdir.py b/chrome/browser/resources/file_manager/bin/squashdir.py index e523a91..408f526 100755 --- a/chrome/browser/resources/file_manager/bin/squashdir.py +++ b/chrome/browser/resources/file_manager/bin/squashdir.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -27,9 +27,11 @@ presented by <input type=file multiple> do not allow users to recurse a selected directory, nor do they provide information about directory structure. """) + def status(msg): sys.stderr.write(msg + '\n') + def scan_path(dest, src, path): abs_src = os.path.join(src, path) statinfo = os.stat(abs_src) @@ -44,10 +46,11 @@ def scan_path(dest, src, path): for child_path in glob.glob(abs_src + '/*'): scan_path(dest, src, child_path[len(src) + 1:]) -if __name__ == '__main__': + +def main(): if len(sys.argv) < 3 or sys.argv[1][0] == '-': usage() - return + return 1 dest = sys.argv[1] for src in sys.argv[2:]: @@ -55,3 +58,8 @@ if __name__ == '__main__': path = os.path.basename(abs_src) abs_src = os.path.dirname(abs_src) scan_path(dest, abs_src, path) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/browser/sync/PRESUBMIT.py b/chrome/browser/sync/PRESUBMIT.py index badc220..aeb3b07 100644 --- a/chrome/browser/sync/PRESUBMIT.py +++ b/chrome/browser/sync/PRESUBMIT.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/browser/ui/views/PRESUBMIT.py b/chrome/browser/ui/views/PRESUBMIT.py index 0a3657e..8b7822a 100755..100644 --- a/chrome/browser/ui/views/PRESUBMIT.py +++ b/chrome/browser/ui/views/PRESUBMIT.py @@ -1,5 +1,4 @@ -#!/usr/bin/python -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/build_nacl_irt.py b/chrome/build_nacl_irt.py index 6225311..be28c10 100755 --- a/chrome/build_nacl_irt.py +++ b/chrome/build_nacl_irt.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -215,13 +215,14 @@ def Main(argv): if args or not options.platforms or ( not options.inputs and not options.outdir): parser.print_help() - sys.exit(1) + return 1 if options.inputs: PrintInputs(options.platforms) else: BuildIRT(options.platforms, options.outdir) + return 0 if __name__ == '__main__': - Main(sys.argv) + sys.exit(Main(sys.argv)) diff --git a/chrome/common/net/PRESUBMIT.py b/chrome/common/net/PRESUBMIT.py index f7a3310..c7e0e21 100644 --- a/chrome/common/net/PRESUBMIT.py +++ b/chrome/common/net/PRESUBMIT.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/installer/util/prebuild/create_string_rc.py b/chrome/installer/util/prebuild/create_string_rc.py index f3acdcd..9e4ddb7 100755 --- a/chrome/installer/util/prebuild/create_string_rc.py +++ b/chrome/installer/util/prebuild/create_string_rc.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -76,6 +76,7 @@ kStringIds = [ # The ID of the first resource string. kFirstResourceID = 1600 + class TranslationStruct: """A helper struct that holds information about a single translation.""" def __init__(self, resource_id_str, language, translation): @@ -152,6 +153,7 @@ def CollectTranslatedStrings(branding): translated_strings.sort() return translated_strings + def WriteRCFile(translated_strings, out_filename): """Writes a resource (rc) file with all the language strings provided in |translated_strings|.""" @@ -177,6 +179,7 @@ def WriteRCFile(translated_strings, out_filename): outfile.write(''.join(lines).encode('utf-16')) outfile.close() + def WriteHeaderFile(translated_strings, out_filename): """Writes a .h file with resource ids. This file can be included by the executable to refer to identifiers.""" @@ -219,7 +222,12 @@ def WriteHeaderFile(translated_strings, out_filename): outfile.write('\n#endif // ndef RC_INVOKED\n') outfile.close() + def main(argv): + # TODO: Use optparse to parse command line flags. + if len(argv) < 2: + print 'Usage:\n %s <output_directory> [branding]' % argv[0] + return 1 branding = '' if (len(sys.argv) > 2): branding = argv[2] @@ -227,10 +235,8 @@ def main(argv): kFilebase = os.path.join(argv[1], 'installer_util_strings') WriteRCFile(translated_strings, kFilebase) WriteHeaderFile(translated_strings, kFilebase) + return 0 + if '__main__' == __name__: - if len(sys.argv) < 2: - print 'Usage:\n %s <output_directory> [branding]' % sys.argv[0] - sys.exit(1) - # Use optparse to parse command line flags. - main(sys.argv) + sys.exit(main(sys.argv)) diff --git a/chrome/nacl/nacl_helper_bootstrap_munge_phdr.py b/chrome/nacl/nacl_helper_bootstrap_munge_phdr.py index 7a53374..2b33050 100755 --- a/chrome/nacl/nacl_helper_bootstrap_munge_phdr.py +++ b/chrome/nacl/nacl_helper_bootstrap_munge_phdr.py @@ -1,20 +1,21 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -# -# This takes three command-line arguments: -# MUNGE-PHDR-PROGRAM file name of program built from -# nacl_helper_bootstrap_munge_phdr.c -# INFILE raw linked ELF file name -# OUTFILE output file name -# -# We just run the MUNGE-PHDR-PROGRAM on a copy of INFILE. -# That modifies the file in place. Then we move it to OUTFILE. -# -# We only have this wrapper script because nacl_helper_bootstrap_munge_phdr.c -# wants to modify a file in place (and it would be a much longer and more -# fragile program if it created a fresh ELF output file instead). + +"""This takes three command-line arguments: + MUNGE-PHDR-PROGRAM file name of program built from + nacl_helper_bootstrap_munge_phdr.c + INFILE raw linked ELF file name + OUTFILE output file name + +We just run the MUNGE-PHDR-PROGRAM on a copy of INFILE. +That modifies the file in place. Then we move it to OUTFILE. + +We only have this wrapper script because nacl_helper_bootstrap_munge_phdr.c +wants to modify a file in place (and it would be a much longer and more +fragile program if it created a fresh ELF output file instead). +""" import shutil import subprocess @@ -24,13 +25,15 @@ import sys def Main(argv): if len(argv) != 4: print 'Usage: %s MUNGE-PHDR-PROGRAM INFILE OUTFILE' % argv[0] - sys.exit(1) + return 1 [prog, munger, infile, outfile] = argv tmpfile = outfile + '.tmp' shutil.copy(infile, tmpfile) segment_num = '2' subprocess.check_call([munger, tmpfile, segment_num]) shutil.move(tmpfile, outfile) + return 0 + if __name__ == '__main__': - Main(sys.argv) + sys.exit(Main(sys.argv)) diff --git a/chrome/test/data/autofill/merge/tools/flatten.py b/chrome/test/data/autofill/merge/tools/flatten.py index 5b3c3de..720329f 100644..100755 --- a/chrome/test/data/autofill/merge/tools/flatten.py +++ b/chrome/test/data/autofill/merge/tools/flatten.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -6,7 +7,6 @@ import sys def main(): """Converts a vertical serialization into a compact, horizontal serialization. - """ COLUMNS = ['First name', 'Middle name', 'Last name', 'Email', 'Company name', @@ -59,7 +59,8 @@ def main(): profile_format = zip(column_formats, profile) profile = [format_.format(value) for (format_, value) in profile_format] print " | ".join(profile) + return 0 if __name__ == '__main__': - main()
\ No newline at end of file + sys.exit(main()) diff --git a/chrome/test/data/autofill/merge/tools/reserialize_profiles_from_query.py b/chrome/test/data/autofill/merge/tools/reserialize_profiles_from_query.py index 44ce711..e57df85 100644..100755 --- a/chrome/test/data/autofill/merge/tools/reserialize_profiles_from_query.py +++ b/chrome/test/data/autofill/merge/tools/reserialize_profiles_from_query.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -6,9 +7,9 @@ import sys from autofill_merge_common import SerializeProfiles, ColumnNameToFieldType + def main(): """Serializes the output of the query 'SELECT * from autofill_profiles;'. - """ COLUMNS = ['GUID', 'LABEL', 'FIRST_NAME', 'MIDDLE_NAME', 'LAST_NAME', 'EMAIL', @@ -32,7 +33,8 @@ def main(): profiles.append(zip(types, values)) print SerializeProfiles(profiles) + return 0 if __name__ == '__main__': - main()
\ No newline at end of file + sys.exit(main()) diff --git a/chrome/test/data/autofill/merge/tools/serialize_profiles.py b/chrome/test/data/autofill/merge/tools/serialize_profiles.py index 5c0c7b1..ac0fa1c 100644..100755 --- a/chrome/test/data/autofill/merge/tools/serialize_profiles.py +++ b/chrome/test/data/autofill/merge/tools/serialize_profiles.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -8,17 +9,18 @@ import sys from autofill_merge_common import SerializeProfiles, ColumnNameToFieldType + def main(): """Serializes the autofill_profiles table from the specified database.""" if len(sys.argv) != 2: print "Usage: python serialize_profiles.py <path/to/database>" - return + return 1 database = sys.argv[1] if not os.path.isfile(database): print "Cannot read database at \"%s\"" % database - return + return 1 # Read the autofill_profile_names table. try: @@ -77,7 +79,8 @@ def main(): profiles[guid].append(("PHONE_HOME_WHOLE_NUMBER", profile[2])) print SerializeProfiles(profiles.values()) + return 0 if __name__ == '__main__': - main()
\ No newline at end of file + sys.exit(main()) diff --git a/chrome/test/nacl_test_injection/buildbot_nacl_integration.py b/chrome/test/nacl_test_injection/buildbot_nacl_integration.py index 8bca104..1636c77 100755 --- a/chrome/test/nacl_test_injection/buildbot_nacl_integration.py +++ b/chrome/test/nacl_test_injection/buildbot_nacl_integration.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/bookmark_model.py b/chrome/test/pyautolib/bookmark_model.py index f67c969..b70fa4a 100644 --- a/chrome/test/pyautolib/bookmark_model.py +++ b/chrome/test/pyautolib/bookmark_model.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/chrome_driver_factory.py b/chrome/test/pyautolib/chrome_driver_factory.py index 362478b..5960f98 100644 --- a/chrome/test/pyautolib/chrome_driver_factory.py +++ b/chrome/test/pyautolib/chrome_driver_factory.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -60,4 +59,3 @@ class ChromeDriverFactory(object): def __del__(self): self.Stop() - diff --git a/chrome/test/pyautolib/chromeos/chromeos_utils.py b/chrome/test/pyautolib/chromeos/chromeos_utils.py index c079c8c..6c0189d 100644..100755 --- a/chrome/test/pyautolib/chromeos/chromeos_utils.py +++ b/chrome/test/pyautolib/chromeos/chromeos_utils.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/chromeos/enable_testing.py b/chrome/test/pyautolib/chromeos/enable_testing.py index 5191ceb..373c764 100644..100755 --- a/chrome/test/pyautolib/chromeos/enable_testing.py +++ b/chrome/test/pyautolib/chromeos/enable_testing.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -19,6 +18,7 @@ Usage: import dbus import optparse import os +import sys class EnableChromeTestingOnChromeOS(object): @@ -46,8 +46,8 @@ class EnableChromeTestingOnChromeOS(object): self.SESSION_MANAGER_PATH), self.SESSION_MANAGER_INTERFACE) print manager.EnableChromeTesting(True, self._options.extra_chrome_flags) + return 0 if __name__ == '__main__': - enabler = EnableChromeTestingOnChromeOS() - enabler.Run() + sys.exit(EnableChromeTestingOnChromeOS().Run()) diff --git a/chrome/test/pyautolib/chromeos/file_browser.py b/chrome/test/pyautolib/chromeos/file_browser.py index f1892e4..8679873 100644 --- a/chrome/test/pyautolib/chromeos/file_browser.py +++ b/chrome/test/pyautolib/chromeos/file_browser.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/chromeos/suid_actions.py b/chrome/test/pyautolib/chromeos/suid_actions.py index 77f771f..824b8cc 100644..100755 --- a/chrome/test/pyautolib/chromeos/suid_actions.py +++ b/chrome/test/pyautolib/chromeos/suid_actions.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -16,6 +15,7 @@ Usage: import optparse import os import shutil +import sys class SuidAction(object): @@ -29,7 +29,6 @@ class SuidAction(object): if not self._options.action: raise RuntimeError('No action specified.') - def Run(self): self._ParseArgs() assert os.geteuid() == 0, 'Needs superuser privileges.' @@ -37,6 +36,7 @@ class SuidAction(object): assert handler and callable(handler), \ 'No handler for %s' % self._options.action handler() + return 0 ## Actions ## @@ -54,5 +54,4 @@ class SuidAction(object): if __name__ == '__main__': - suid_action = SuidAction() - suid_action.Run() + sys.exit(SuidAction().Run()) diff --git a/chrome/test/pyautolib/chromeos_network.py b/chrome/test/pyautolib/chromeos_network.py index 01d416d..d2b8a66 100644 --- a/chrome/test/pyautolib/chromeos_network.py +++ b/chrome/test/pyautolib/chromeos_network.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/chromoting.py b/chrome/test/pyautolib/chromoting.py index 8d642d6..5e0f5ee 100644 --- a/chrome/test/pyautolib/chromoting.py +++ b/chrome/test/pyautolib/chromoting.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/download_info.py b/chrome/test/pyautolib/download_info.py index 6be61b0..e13eae7 100644 --- a/chrome/test/pyautolib/download_info.py +++ b/chrome/test/pyautolib/download_info.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/fetch_prebuilt_pyauto.py b/chrome/test/pyautolib/fetch_prebuilt_pyauto.py index 9b4c367..a983312 100644..100755 --- a/chrome/test/pyautolib/fetch_prebuilt_pyauto.py +++ b/chrome/test/pyautolib/fetch_prebuilt_pyauto.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -172,7 +171,8 @@ class FetchPrebuilt(object): os.symlink(framework, dest) print 'Prepared binaries in "%s"' % self._outdir + return 0 if __name__ == '__main__': - FetchPrebuilt().Run() + sys.exit(FetchPrebuilt().Run()) diff --git a/chrome/test/pyautolib/generate_docs.py b/chrome/test/pyautolib/generate_docs.py index 352b453..a603ad8 100644..100755 --- a/chrome/test/pyautolib/generate_docs.py +++ b/chrome/test/pyautolib/generate_docs.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -36,7 +36,7 @@ def main(): if options.dir == os.getcwd(): print 'Export complete, files are located in %s' % options.dir - return + return 1 new_files = current_contents.difference(previous_contents) for file_name in new_files: @@ -50,8 +50,8 @@ def main(): shutil.move(full_path, options.dir) print 'Export complete, files are located in %s' % options.dir + return 0 if __name__ == '__main__': - main() - + sys.exit(main()) diff --git a/chrome/test/pyautolib/history_info.py b/chrome/test/pyautolib/history_info.py index 7407f05..bf500a0 100644 --- a/chrome/test/pyautolib/history_info.py +++ b/chrome/test/pyautolib/history_info.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/omnibox_info.py b/chrome/test/pyautolib/omnibox_info.py index ac6b16a..216e604 100644 --- a/chrome/test/pyautolib/omnibox_info.py +++ b/chrome/test/pyautolib/omnibox_info.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -141,4 +139,3 @@ class OmniboxInfo(object): def IsQueryInProgress(self): """Determine if a query is in progress.""" return self.Properties('query_in_progress') - diff --git a/chrome/test/pyautolib/perf_snapshot.py b/chrome/test/pyautolib/perf_snapshot.py index 2b5d15e..fff9333 100644..100755 --- a/chrome/test/pyautolib/perf_snapshot.py +++ b/chrome/test/pyautolib/perf_snapshot.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -870,7 +869,8 @@ def main(): snapshotter.SetInteractiveMode() snapshotter.HeapSnapshot() + return 0 if __name__ == '__main__': - main() + sys.exit(main()) diff --git a/chrome/test/pyautolib/plugins_info.py b/chrome/test/pyautolib/plugins_info.py index 6cf8620..109981d 100644 --- a/chrome/test/pyautolib/plugins_info.py +++ b/chrome/test/pyautolib/plugins_info.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -112,4 +110,3 @@ class PluginsInfo(object): all = self.PluginForName(name) if not all: return None return all[0] - diff --git a/chrome/test/pyautolib/prefs_info.py b/chrome/test/pyautolib/prefs_info.py index eefe679..ce58be5 100644 --- a/chrome/test/pyautolib/prefs_info.py +++ b/chrome/test/pyautolib/prefs_info.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/pyauto.py b/chrome/test/pyautolib/pyauto.py index 9e179f6..15b81f6 100644..100755 --- a/chrome/test/pyautolib/pyauto.py +++ b/chrome/test/pyautolib/pyauto.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/pyauto_errors.py b/chrome/test/pyautolib/pyauto_errors.py index 86a56f0..6957fdc 100644 --- a/chrome/test/pyautolib/pyauto_errors.py +++ b/chrome/test/pyautolib/pyauto_errors.py @@ -1,6 +1,4 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/pyauto_paths.py b/chrome/test/pyautolib/pyauto_paths.py index 71615b1..930c010 100644 --- a/chrome/test/pyautolib/pyauto_paths.py +++ b/chrome/test/pyautolib/pyauto_paths.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/pyauto_utils.py b/chrome/test/pyautolib/pyauto_utils.py index 0a13b67..eb954eea 100644 --- a/chrome/test/pyautolib/pyauto_utils.py +++ b/chrome/test/pyautolib/pyauto_utils.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/pyauto_utils_test.py b/chrome/test/pyautolib/pyauto_utils_test.py index f612ba7..3a3a85c 100644..100755 --- a/chrome/test/pyautolib/pyauto_utils_test.py +++ b/chrome/test/pyautolib/pyauto_utils_test.py @@ -1,6 +1,5 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/remote_host.py b/chrome/test/pyautolib/remote_host.py index ba84155..c51a6e0 100644..100755 --- a/chrome/test/pyautolib/remote_host.py +++ b/chrome/test/pyautolib/remote_host.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/pyautolib/timer_queue.py b/chrome/test/pyautolib/timer_queue.py index 8985803..b7d668d 100644 --- a/chrome/test/pyautolib/timer_queue.py +++ b/chrome/test/pyautolib/timer_queue.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -85,4 +83,3 @@ class TimerQueue(threading.Thread): if self.terminate: return time.sleep(self.wait_time) - diff --git a/chrome/test/webdriver/test/chromedriver_factory.py b/chrome/test/webdriver/test/chromedriver_factory.py index ff9d765..3f96947 100644 --- a/chrome/test/webdriver/test/chromedriver_factory.py +++ b/chrome/test/webdriver/test/chromedriver_factory.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/chromedriver_launcher.py b/chrome/test/webdriver/test/chromedriver_launcher.py index 57a83a2..c4c17ae 100644 --- a/chrome/test/webdriver/test/chromedriver_launcher.py +++ b/chrome/test/webdriver/test/chromedriver_launcher.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/chromedriver_server.py b/chrome/test/webdriver/test/chromedriver_server.py index 6e9729b..a04f6da 100644 --- a/chrome/test/webdriver/test/chromedriver_server.py +++ b/chrome/test/webdriver/test/chromedriver_server.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/chromedriver_test.py b/chrome/test/webdriver/test/chromedriver_test.py index 53002f7..b3206bb 100644 --- a/chrome/test/webdriver/test/chromedriver_test.py +++ b/chrome/test/webdriver/test/chromedriver_test.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/chromedriver_tests.py b/chrome/test/webdriver/test/chromedriver_tests.py index 92f8357..acaef6b 100644 --- a/chrome/test/webdriver/test/chromedriver_tests.py +++ b/chrome/test/webdriver/test/chromedriver_tests.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/py_unittest_util.py b/chrome/test/webdriver/test/py_unittest_util.py index 3adba64..13d0e5b 100644 --- a/chrome/test/webdriver/test/py_unittest_util.py +++ b/chrome/test/webdriver/test/py_unittest_util.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/run_chromedriver_tests.py b/chrome/test/webdriver/test/run_chromedriver_tests.py index 3f74afe..8d1aeff 100644..100755 --- a/chrome/test/webdriver/test/run_chromedriver_tests.py +++ b/chrome/test/webdriver/test/run_chromedriver_tests.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -24,7 +24,7 @@ import chromedriver_tests import py_unittest_util -if __name__ == '__main__': +def main(): parser = optparse.OptionParser() parser.add_option( '', '--filter', type='string', default='*', @@ -47,10 +47,10 @@ if __name__ == '__main__': if options.list is True: print '\n'.join(py_unittest_util.GetTestNamesFromSuite(filtered_suite)) - sys.exit(0) + return 0 if sys.platform.startswith('darwin'): print 'All tests temporarily disabled on mac, crbug.com/103434' - sys.exit(0) + return 0 driver_exe = options.driver_exe if driver_exe is not None: @@ -61,4 +61,8 @@ if __name__ == '__main__': ChromeDriverTest.GlobalSetUp(driver_exe, chrome_exe) result = py_unittest_util.GTestTextTestRunner(verbosity=1).run(filtered_suite) ChromeDriverTest.GlobalTearDown() - sys.exit(not result.wasSuccessful()) + return not result.wasSuccessful() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/test/webdriver/test/run_webdriver_tests.py b/chrome/test/webdriver/test/run_webdriver_tests.py index 6b78862..78a8a88 100644..100755 --- a/chrome/test/webdriver/test/run_webdriver_tests.py +++ b/chrome/test/webdriver/test/run_webdriver_tests.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/test_paths.py b/chrome/test/webdriver/test/test_paths.py index 2af0703..665e33a 100644 --- a/chrome/test/webdriver/test/test_paths.py +++ b/chrome/test/webdriver/test/test_paths.py @@ -1,5 +1,3 @@ -#!/usr/bin/python - # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/test/webdriver/test/util.py b/chrome/test/webdriver/test/util.py index 43f0518..ec60611 100644 --- a/chrome/test/webdriver/test/util.py +++ b/chrome/test/webdriver/test/util.py @@ -1,4 +1,3 @@ -#!/usr/bin/python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/third_party/jstemplate/compile.py b/chrome/third_party/jstemplate/compile.py index 3d16d96..3e75807 100755 --- a/chrome/third_party/jstemplate/compile.py +++ b/chrome/third_party/jstemplate/compile.py @@ -1,38 +1,44 @@ #!/usr/bin/env python -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -# A python script that combines the javascript files needed by jstemplate into -# a single file. +"""Combines the javascript files needed by jstemplate into a single file.""" import httplib import urllib -srcs ="util.js jsevalcontext.js jstemplate.js exports.js".split() -out = "jstemplate_compiled.js" - -# Wrap the output in an anonymous function to prevent poluting the global -# namespace. -output_wrapper = "(function(){%s})()" - -# Define the parameters for the POST request and encode them in a URL-safe -# format. See http://code.google.com/closure/compiler/docs/api-ref.html for API -# reference. -params = urllib.urlencode( - map(lambda src: ('js_code', file(src).read()), srcs) + - [ - ('compilation_level', 'ADVANCED_OPTIMIZATIONS'), - ('output_format', 'text'), - ('output_info', 'compiled_code'), - ]) - -# Always use the following value for the Content-type header. -headers = {'Content-type': 'application/x-www-form-urlencoded'} -conn = httplib.HTTPConnection('closure-compiler.appspot.com') -conn.request('POST', '/compile', params, headers) -response = conn.getresponse() -out_file = file(out, 'w') -out_file.write(output_wrapper % response.read()) -out_file.close() -conn.close() + +def main(): + srcs = ['util.js', 'jsevalcontext.js', 'jstemplate.js', 'exports.js'] + out = 'jstemplate_compiled.js' + + # Wrap the output in an anonymous function to prevent poluting the global + # namespace. + output_wrapper = '(function(){%s})()' + + # Define the parameters for the POST request and encode them in a URL-safe + # format. See http://code.google.com/closure/compiler/docs/api-ref.html for + # API reference. + params = urllib.urlencode( + map(lambda src: ('js_code', file(src).read()), srcs) + + [ + ('compilation_level', 'ADVANCED_OPTIMIZATIONS'), + ('output_format', 'text'), + ('output_info', 'compiled_code'), + ]) + + # Always use the following value for the Content-type header. + headers = {'Content-type': 'application/x-www-form-urlencoded'} + conn = httplib.HTTPConnection('closure-compiler.appspot.com') + conn.request('POST', '/compile', params, headers) + response = conn.getresponse() + out_file = file(out, 'w') + out_file.write(output_wrapper % response.read()) + out_file.close() + conn.close() + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/tools/automated_ui_test_tools/ui_action_generator.py b/chrome/tools/automated_ui_test_tools/ui_action_generator.py index 098a68d..b455554 100644..100755 --- a/chrome/tools/automated_ui_test_tools/ui_action_generator.py +++ b/chrome/tools/automated_ui_test_tools/ui_action_generator.py @@ -1,5 +1,4 @@ -#!/usr/bin/python - +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -68,10 +67,7 @@ def CreateUIActionList(actions_per_command, num_commands, given_seed=None): def ParseCommandLine(): - """Parses the command line. - - Returns: - List of options and their values, and unparsed args. + """Returns the list of options and their values, and unparsed args. """ parser = optparse.OptionParser() parser.add_option('-o', '--output', dest='output_file', type='string', @@ -102,7 +98,8 @@ def main(): f.write(command_list) f.close() print command_list + return 0 if __name__ == '__main__': - main() + sys.exit(main()) diff --git a/chrome/tools/build/appid.py b/chrome/tools/build/appid.py index f471a4e..d052bc3 100644..100755 --- a/chrome/tools/build/appid.py +++ b/chrome/tools/build/appid.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/tools/build/generate_policy_source.py b/chrome/tools/build/generate_policy_source.py index ad234f2..e405713 100644..100755 --- a/chrome/tools/build/generate_policy_source.py +++ b/chrome/tools/build/generate_policy_source.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -51,7 +51,7 @@ def main(): if len(args) != 3: print "exactly platform, chromium_os flag and input file must be specified." parser.print_help() - sys.exit(2) + return 2 template_file_contents = _LoadJSONFile(args[2]); if opts.header_path is not None: _WritePolicyConstantHeader(template_file_contents, args, opts); @@ -63,6 +63,7 @@ def main(): _WriteProtobuf(template_file_contents, args, opts.proto_path) if opts.decoder_path is not None: _WriteProtobufParser(template_file_contents, args, opts.decoder_path) + return 0 #------------------ shared helpers ---------------------------------# @@ -462,6 +463,5 @@ def _WriteProtobufParser(template_file_contents, args, outfilepath): f.write(CPP_FOOT) -#------------------ main() -----------------------------------------# if __name__ == '__main__': - main(); + sys.exit(main()) diff --git a/chrome/tools/build/win/create_installer_archive.py b/chrome/tools/build/win/create_installer_archive.py index 2b59fa0..5a4e96c 100755 --- a/chrome/tools/build/win/create_installer_archive.py +++ b/chrome/tools/build/win/create_installer_archive.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/tools/build/win/dependencies.py b/chrome/tools/build/win/dependencies.py index 353c89c..01d9254 100755 --- a/chrome/tools/build/win/dependencies.py +++ b/chrome/tools/build/win/dependencies.py @@ -1,5 +1,5 @@ -#!/usr/bin/python -# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -40,6 +40,7 @@ def RunSystemCommand(cmd): except: raise Error("Failed to execute: " + cmd) + def RunDumpbin(binary_file): """Runs dumpbin and parses its output. @@ -196,8 +197,19 @@ def VerifyDependents(pe_name, dependents, delay_loaded, list_file, verbose): return max(deps_result, delayed_result) -def main(options, args): +def main(): # PE means portable executable. It's any .DLL, .EXE, .SYS, .AX, etc. + usage = "usage: %prog [options] input output" + option_parser = optparse.OptionParser(usage=usage) + option_parser.add_option("-d", + "--debug", + dest="debug", + action="store_true", + default=False, + help="Display debugging information") + options, args = option_parser.parse_args() + if len(args) != 2: + option_parser.error("Incorrect number of arguments") pe_name = args[0] deps_file = args[1] dependents, delay_loaded = RunDumpbin(pe_name) @@ -211,15 +223,4 @@ def main(options, args): if '__main__' == __name__: - usage = "usage: %prog [options] input output" - option_parser = optparse.OptionParser(usage = usage) - option_parser.add_option("-d", - "--debug", - dest="debug", - action="store_true", - default=False, - help="Display debugging information") - options, args = option_parser.parse_args() - if len(args) != 2: - option_parser.error("Incorrect number of arguments") - sys.exit(main(options, args)) + sys.exit(main()) diff --git a/chrome/tools/build/win/make_policy_zip.py b/chrome/tools/build/win/make_policy_zip.py index 60037c3..0822483 100644..100755 --- a/chrome/tools/build/win/make_policy_zip.py +++ b/chrome/tools/build/win/make_policy_zip.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -12,6 +13,7 @@ import os import sys import zipfile + def add_files_to_zip(zip_file, base_dir, file_list): """Pack a list of files into a zip archive, that is already opened for writing. @@ -26,6 +28,7 @@ def add_files_to_zip(zip_file, base_dir, file_list): zip_file.write(base_dir + file_path, file_path) return 0 + def get_grd_outputs(grit_cmd, grit_defines, grd_file, grd_strip_path_prefix): grit_path = os.path.join(os.getcwd(), os.path.dirname(grit_cmd)) sys.path.append(grit_path) @@ -37,6 +40,7 @@ def get_grd_outputs(grit_cmd, grit_defines, grd_file, grd_strip_path_prefix): result.append(item[len(grd_strip_path_prefix):]) return result + def main(argv): """Pack a list of files into a zip archive. @@ -73,6 +77,6 @@ def main(argv): finally: zip_file.close() + if '__main__' == __name__: sys.exit(main(sys.argv)) - diff --git a/chrome/tools/build/win/scan_server_dlls.py b/chrome/tools/build/win/scan_server_dlls.py index 2adf8d6..68ebb93 100644..100755 --- a/chrome/tools/build/win/scan_server_dlls.py +++ b/chrome/tools/build/win/scan_server_dlls.py @@ -1,12 +1,11 @@ -#!/usr/bin/python -# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Script used to scan for server DLLs at build time and build a header included by setup.exe. This header contains an array of the names of the DLLs that need registering at install time. - """ import ConfigParser @@ -128,19 +127,11 @@ def RunSystemCommand(cmd): raise "Error while running cmd: %s" % cmd -def main(options): +def main(): """Main method that reads input file, scans <build_output>\servers for matches to files described in the input file. A header file for the setup project is then generated. """ - config = Readconfig(options.output_dir, options.input_file) - registered_dll_list = ScanServerDlls(config, options.distribution, - options.output_dir) - CreateRegisteredDllIncludeFile(registered_dll_list, - options.header_output_dir) - - -if '__main__' == __name__: option_parser = optparse.OptionParser() option_parser.add_option('-o', '--output_dir', help='Build Output directory') option_parser.add_option('-x', '--header_output_dir', @@ -150,4 +141,13 @@ if '__main__' == __name__: help='Name of Chromium Distribution. Optional.') options, args = option_parser.parse_args() - sys.exit(main(options)) + config = Readconfig(options.output_dir, options.input_file) + registered_dll_list = ScanServerDlls(config, options.distribution, + options.output_dir) + CreateRegisteredDllIncludeFile(registered_dll_list, + options.header_output_dir) + return 0 + + +if '__main__' == __name__: + sys.exit(main()) diff --git a/chrome/tools/build/win/sln_deps.py b/chrome/tools/build/win/sln_deps.py index ef7803e..ebb371a 100755 --- a/chrome/tools/build/win/sln_deps.py +++ b/chrome/tools/build/win/sln_deps.py @@ -1,5 +1,5 @@ -#!/usr/bin/python -# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -63,7 +63,7 @@ def ScanSlnFile(filename): return projects -def main(filename, project_to_scan, reverse): +def sln_deps(filename, project_to_scan, reverse): """Displays the project's dependencies.""" project_to_scan = project_to_scan.lower() @@ -91,9 +91,10 @@ def main(filename, project_to_scan, reverse): deps_name = [projects[d].name for d in project.deps] print "\n".join(str(" " + name) for name in sorted(deps_name, key=str.lower)) + return 0 -if __name__ == '__main__': +def main(): usage = "usage: %prog [options] solution [project]" description = ("Display the dependencies of a project in human readable" @@ -116,5 +117,8 @@ if __name__ == '__main__': project_to_scan = "" if len(args) == 2: project_to_scan = args[1] - main(args[0], project_to_scan, options.reverse) + return sln_deps(args[0], project_to_scan, options.reverse) + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/tools/build/win/sort_sln.py b/chrome/tools/build/win/sort_sln.py deleted file mode 100755 index ea88ce4..0000000 --- a/chrome/tools/build/win/sort_sln.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/python -# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -import sys - -if len(sys.argv) != 2: - print """Usage: sort_sln.py <SOLUTIONNAME>.sln -to sort the solution file to a normalized scheme. Do this before checking in -changes to a solution file to avoid having a lot of unnecessary diffs.""" - sys.exit(1) - -filename = sys.argv[1] -print "Sorting " + filename; - -try: - sln = open(filename, "r"); -except IOError: - print "Unable to open " + filename + " for reading." - sys.exit(1) - -output = "" -seclines = None -while 1: - line = sln.readline() - if not line: - break - - if seclines is not None: - # Process the end of a section, dump the sorted lines - if line.lstrip().startswith('End'): - output = output + ''.join(sorted(seclines)) - seclines = None - # Process within a section - else: - seclines.append(line) - continue - - # Process the start of a section - if (line.lstrip().startswith('GlobalSection') or - line.lstrip().startswith('ProjectSection')): - if seclines: raise Exception('Already in a section') - seclines = [] - - output = output + line - -sln.close() -try: - sln = open(filename, "w") - sln.write(output) -except IOError: - print "Unable to write to " + filename - sys.exit(1); -print "Done." - diff --git a/chrome/tools/check_grd_for_unused_strings.py b/chrome/tools/check_grd_for_unused_strings.py index 3bc57d7..b0f8cb8 100755 --- a/chrome/tools/check_grd_for_unused_strings.py +++ b/chrome/tools/check_grd_for_unused_strings.py @@ -1,6 +1,5 @@ -#!/usr/bin/python - -# Copyright (c) 2010 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -111,7 +110,7 @@ def CheckForUnusedGrdIDsInSources(grd_files, src_dirs): return 0 -if __name__ == '__main__': +def main(): # script lives in src/chrome/tools chrome_tools_dir = os.path.dirname(os.path.abspath(sys.argv[0])) src_dir = os.path.dirname(os.path.dirname(chrome_tools_dir)) @@ -162,4 +161,8 @@ if __name__ == '__main__': os.path.join(src_dir, 'third_party', 'mozilla_security_manager'), ] - sys.exit(CheckForUnusedGrdIDsInSources(grd_files, src_dirs)) + return CheckForUnusedGrdIDsInSources(grd_files, src_dirs) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/tools/extract_actions.py b/chrome/tools/extract_actions.py index 650f6e4..09bab23 100755 --- a/chrome/tools/extract_actions.py +++ b/chrome/tools/extract_actions.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -439,6 +439,8 @@ def main(argv): if hash_output: print "Done. Do not forget to add chromeactions.txt to your changelist" + return 0 + if '__main__' == __name__: - main(sys.argv) + sys.exit(main(sys.argv)) diff --git a/chrome/tools/extract_histograms.py b/chrome/tools/extract_histograms.py index 8c002f3..82fc9e9 100755 --- a/chrome/tools/extract_histograms.py +++ b/chrome/tools/extract_histograms.py @@ -1,5 +1,5 @@ -#!/usr/bin/python -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -35,6 +35,7 @@ def GrepForHistograms(path, histograms): if match: histograms.add(match.group(1)) + def WalkDirectory(root_path, histograms): for path, dirs, files in os.walk(root_path): if '.svn' in dirs: @@ -44,6 +45,7 @@ def WalkDirectory(root_path, histograms): if ext == '.cc': GrepForHistograms(os.path.join(path, file), histograms) + def main(argv): histograms = set() @@ -53,6 +55,8 @@ def main(argv): # Print out the histograms as a sorted list. for histogram in sorted(histograms): print histogram + return 0 + if '__main__' == __name__: - main(sys.argv) + sys.exit(main(sys.argv)) diff --git a/chrome/tools/history-viz.py b/chrome/tools/history-viz.py index 6f82126..fccbb31 100755 --- a/chrome/tools/history-viz.py +++ b/chrome/tools/history-viz.py @@ -1,5 +1,5 @@ -#!/usr/bin/python -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -17,7 +17,18 @@ import subprocess import sys import urlparse -class URL: + +# Some transition types, copied from page_transition_types.h. +TRANS_TYPES = { + 0: 'link', + 1: 'typed', + 2: 'most-visited', + 3: 'auto subframe', + 7: 'form', +} + + +class URL(object): """Represents a broken-down URL from our most visited database.""" def __init__(self, id, url): @@ -67,7 +78,8 @@ class URL: lines.append(line) return '\n'.join(lines) -class Edge: + +class Edge(object): """Represents an edge in the history graph, connecting two pages. If a link is traversed twice, it is one Edge with two entries in @@ -97,6 +109,7 @@ class Edge: # edge['chain'] = chain return all + def ClusterBy(objs, pred): """Group a list of objects by a predicate. @@ -109,12 +122,14 @@ def ClusterBy(objs, pred): clusters[cluster].append(obj) return clusters -def EscapeDot(str): + +def EscapeDot(string): """Escape a string suitable for embedding in a graphviz graph.""" # TODO(evanm): this is likely not sufficient. - return str.replace('\n', '\\n') + return string.replace('\n', '\\n') + -class SQLite: +class SQLite(object): """Trivial interface to executing SQLite queries. Spawns a new process with each call.""" def __init__(self, file=None): @@ -132,6 +147,7 @@ class SQLite: row = line.strip().split('\t') yield row + def LoadHistory(filename): db = SQLite(filename) @@ -157,85 +173,81 @@ def LoadHistory(filename): return urls, edges -# Some transition types, copied from page_transition_types.h. -TRANS_TYPES = { - 0: 'link', - 1: 'typed', - 2: 'most-visited', - 3: 'auto subframe', - 7: 'form', -} -urls, edges = LoadHistory(sys.argv[1]) - -print 'digraph G {' -print ' graph [rankdir=LR]' # Display left to right. -print ' node [shape=box]' # Display nodes as boxes. -print ' subgraph { rank=source; 0 [label="start"] }' - -# Output all the nodes within graph clusters. -hosts = ClusterBy(urls.values(), lambda url: url.host) -for i, (host, urls) in enumerate(hosts.items()): - # Cluster all URLs under this host if it has more than one entry. - host_clustered = len(urls) > 1 - if host_clustered: - print 'subgraph clusterhost%d {' % i - print ' label="%s"' % host - paths = ClusterBy(urls, lambda url: url.path) - for j, (path, urls) in enumerate(paths.items()): - # Cluster all URLs under this host if it has more than one entry. - path_clustered = host_clustered and len(urls) > 1 - if path_clustered: - print ' subgraph cluster%d%d {' % (i, j) - print ' label="%s"' % path - for url in urls: - if url.id == '0': continue # We already output the special start node. - pretty = url.PrettyPrint(include_host=not host_clustered, - include_path=not path_clustered) - print ' %s [label="%s"]' % (url.id, EscapeDot(pretty)) - if path_clustered: - print ' }' - if host_clustered: - print '}' - -# Output all the edges between nodes. -for src, dsts in edges.items(): - for dst, edge in dsts.items(): - # Gather up all the transitions into the label. - label = [] # Label for the edge. - transitions = edge.Transitions() - for trans, count in transitions.items(): - text = '' - if count > 1: - text = '%dx ' % count - base_type = trans & 0xFF - redir = (trans & 0xC0000000) != 0 - start = (trans & 0x10000000) != 0 - end = (trans & 0x20000000) != 0 - if start or end: - if start: - text += '<' - if end: - text += '>' - text += ' ' - if redir: - text += 'R ' - text += TRANS_TYPES.get(base_type, 'trans%d' % base_type) - label.append(text) - if len(label) == 0: - continue - - edgeattrs = [] # Graphviz attributes for the edge. - # If the edge is from the start and the transitions are fishy, make it - # display as a dotted line. - if src == '0' and len(transitions.keys()) == 1 and transitions.has_key(0): - edgeattrs.append('style=dashed') - if len(label) > 0: - edgeattrs.append('label="%s"' % EscapeDot('\n'.join(label))) - - out = '%s -> %s' % (src, dst) - if len(edgeattrs) > 0: - out += ' [%s]' % ','.join(edgeattrs) - print out -print '}' +def main(): + urls, edges = LoadHistory(sys.argv[1]) + print 'digraph G {' + print ' graph [rankdir=LR]' # Display left to right. + print ' node [shape=box]' # Display nodes as boxes. + print ' subgraph { rank=source; 0 [label="start"] }' + # Output all the nodes within graph clusters. + hosts = ClusterBy(urls.values(), lambda url: url.host) + for i, (host, urls) in enumerate(hosts.items()): + # Cluster all URLs under this host if it has more than one entry. + host_clustered = len(urls) > 1 + if host_clustered: + print 'subgraph clusterhost%d {' % i + print ' label="%s"' % host + paths = ClusterBy(urls, lambda url: url.path) + for j, (path, urls) in enumerate(paths.items()): + # Cluster all URLs under this host if it has more than one entry. + path_clustered = host_clustered and len(urls) > 1 + if path_clustered: + print ' subgraph cluster%d%d {' % (i, j) + print ' label="%s"' % path + for url in urls: + if url.id == '0': continue # We already output the special start node. + pretty = url.PrettyPrint(include_host=not host_clustered, + include_path=not path_clustered) + print ' %s [label="%s"]' % (url.id, EscapeDot(pretty)) + if path_clustered: + print ' }' + if host_clustered: + print '}' + + # Output all the edges between nodes. + for src, dsts in edges.items(): + for dst, edge in dsts.items(): + # Gather up all the transitions into the label. + label = [] # Label for the edge. + transitions = edge.Transitions() + for trans, count in transitions.items(): + text = '' + if count > 1: + text = '%dx ' % count + base_type = trans & 0xFF + redir = (trans & 0xC0000000) != 0 + start = (trans & 0x10000000) != 0 + end = (trans & 0x20000000) != 0 + if start or end: + if start: + text += '<' + if end: + text += '>' + text += ' ' + if redir: + text += 'R ' + text += TRANS_TYPES.get(base_type, 'trans%d' % base_type) + label.append(text) + if len(label) == 0: + continue + + edgeattrs = [] # Graphviz attributes for the edge. + # If the edge is from the start and the transitions are fishy, make it + # display as a dotted line. + if src == '0' and len(transitions.keys()) == 1 and transitions.has_key(0): + edgeattrs.append('style=dashed') + if len(label) > 0: + edgeattrs.append('label="%s"' % EscapeDot('\n'.join(label))) + + out = '%s -> %s' % (src, dst) + if len(edgeattrs) > 0: + out += ' [%s]' % ','.join(edgeattrs) + print out + print '}' + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/chrome/tools/inconsistent-eol.py b/chrome/tools/inconsistent-eol.py index 4ab3596..ef25245 100755 --- a/chrome/tools/inconsistent-eol.py +++ b/chrome/tools/inconsistent-eol.py @@ -1,5 +1,5 @@ -#!/usr/bin/python -# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -38,6 +38,7 @@ def CountChars(text, str): logging.debug(len(split) - 1) return len(split) - 1 + def PrevailingEOLName(crlf, cr, lf): """Describe the most common line ending. @@ -56,6 +57,7 @@ def PrevailingEOLName(crlf, cr, lf): return 'crlf' return 'lf' + def FixEndings(file, crlf, cr, lf): """Change the file's line endings to CRLF or LF, whichever is more common.""" most = max(crlf, cr, lf) @@ -99,7 +101,8 @@ def ProcessFiles(filelist): print '%s: mostly %s' % (filename, PrevailingEOLName(crlf, cr, lf)) FixEndings(filename, crlf, cr, lf) -def main(options, args): + +def process(options, args): """Process the files.""" if not args or len(args) < 1: raise Error('No files given.') @@ -111,8 +114,10 @@ def main(options, args): else: filelist = args ProcessFiles(filelist) + return 0 -if '__main__' == __name__: + +def main(): if DEBUGGING: debug_level = logging.DEBUG else: @@ -131,5 +136,8 @@ if '__main__' == __name__: default=False, help="Force any files with CRLF to LF instead.") options, args = option_parser.parse_args() + return process(options, args) + - sys.exit(main(options, args)) +if '__main__' == __name__: + sys.exit(main()) diff --git a/chrome/tools/process_dumps_linux.py b/chrome/tools/process_dumps_linux.py index 3e3bf3e..1f0ba9d 100755 --- a/chrome/tools/process_dumps_linux.py +++ b/chrome/tools/process_dumps_linux.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -265,7 +265,9 @@ def main_linux(options, args): return 0 -if '__main__' == __name__: +def main(): + if not sys.platform.startswith('linux'): + return 1 parser = optparse.OptionParser() parser.add_option('', '--processor-dir', type='string', default='', help='The directory where the processor is installed. ' @@ -291,8 +293,8 @@ if '__main__' == __name__: 'Default: chrome') (options, args) = parser.parse_args() + return main_linux(options, args) - if sys.platform.startswith('linux'): - sys.exit(main_linux(options, args)) - else: - sys.exit(1) + +if '__main__' == __name__: + sys.exit(main()) diff --git a/chrome/tools/webforms_aggregator.py b/chrome/tools/webforms_aggregator.py index 3d5327b..16e5273 100644..100755 --- a/chrome/tools/webforms_aggregator.py +++ b/chrome/tools/webforms_aggregator.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -722,7 +722,6 @@ class ThreadedCrawler(object): def main(): - # Command line options. usage = 'usage: %prog [options] single_url_or_urls_filename' parser = optparse.OptionParser(usage) parser.add_option( @@ -734,7 +733,7 @@ def main(): if options.log_level not in ['DEBUG', 'INFO', 'WARNING', 'ERROR']: print 'Wrong log_level argument.' parser.print_help() - sys.exit(1) + return 1 options.log_level = getattr(logging, options.log_level) if len(args) != 1: @@ -762,7 +761,8 @@ def main(): logger.info('Started at: %s\n', t0) logger.info('Ended at: %s\n', t1) logger.info('Total execution time: %s\n', delta_t) + return 0 if __name__ == "__main__": - main() + sys.exit(main()) diff --git a/chrome/tools/webforms_aggregator_tests.py b/chrome/tools/webforms_aggregator_tests.py index fc12dc3..2eb26bb 100644..100755 --- a/chrome/tools/webforms_aggregator_tests.py +++ b/chrome/tools/webforms_aggregator_tests.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/tools/webforms_aggregator_unittests.py b/chrome/tools/webforms_aggregator_unittests.py index 68169eb..00ea2bd 100644..100755 --- a/chrome/tools/webforms_aggregator_unittests.py +++ b/chrome/tools/webforms_aggregator_unittests.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/chrome/tools/webforms_extractor.py b/chrome/tools/webforms_extractor.py index 71fed7c..1dd1d95 100644..100755 --- a/chrome/tools/webforms_extractor.py +++ b/chrome/tools/webforms_extractor.py @@ -1,253 +1,254 @@ -#!/usr/bin/python
-# Copyright (c) 2011 The Chromium Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be found
-# in the LICENSE file.
-
-"""Extracts registration forms from the corresponding HTML files.
-
-Used for extracting forms within HTML files. This script is used in
-conjunction with the webforms_aggregator.py script, which aggregates web pages
-with fillable forms (i.e registration forms).
-
-The purpose of this script is to extract out all non-form elements that may be
-causing parsing errors and timeout issues when running browser_tests.
-
-This script extracts all forms from a HTML file.
-If there are multiple forms per downloaded site, multiple files are created
-for each form.
-
-Used as a standalone script but assumes that it is run from the directory in
-which it is checked into.
-
-Usage: forms_extractor.py [options]
-
-Options:
- -l LOG_LEVEL, --log_level=LOG_LEVEL,
- LOG_LEVEL: debug, info, warning or error [default: error]
- -j, --js extracts javascript elements from web form.
- -h, --help show this help message and exit
-"""
-
-import glob
-import logging
-from optparse import OptionParser
-import os
-import re
-import sys
-
-
-class FormsExtractor(object):
- """Extracts HTML files, leaving only registration forms from the HTML file."""
- _HTML_FILES_PATTERN = r'*.html'
- _HTML_FILE_PREFIX = r'grabber-'
- _FORM_FILE_PREFIX = r'grabber-stripped-'
-
- _REGISTRATION_PAGES_DIR = os.path.join(os.pardir, 'test', 'data', 'autofill',
- 'heuristics', 'input')
- _EXTRACTED_FORMS_DIR = os.path.join(os.pardir, 'test', 'data', 'autofill',
- 'heuristics', 'input')
-
- logger = logging.getLogger(__name__)
- log_handlers = {'StreamHandler': None}
-
- # This pattern is used for retrieving the form location comment located at the
- # top of each downloaded HTML file indicating where the form originated from.
- _RE_FORM_LOCATION_PATTERN = re.compile(
- ur"""
- <!--Form\s{1}Location: # Starting of form location comment.
- .*? # Any characters (non-greedy).
- --> # Ending of the form comment.
- """, re.U | re.S | re.I | re.X)
-
- # This pattern is used for removing all script code.
- _RE_SCRIPT_PATTERN = re.compile(
- ur"""
- <script # A new opening '<script' tag.
- \b # The end of the word 'script'.
- .*? # Any characters (non-greedy).
- > # Ending of the (opening) tag: '>'.
- .*? # Any characters (non-greedy) between the tags.
- </script\s*> # The '</script>' closing tag.
- """, re.U | re.S | re.I | re.X)
-
- # This pattern is used for removing all href js code.
- _RE_HREF_JS_PATTERN = re.compile(
- ur"""
- \bhref # The word href and its beginning.
- \s*=\s* # The '=' with all whitespace before and after it.
- (?P<quote>[\'\"]) # A single or double quote which is captured.
- \s*javascript\s*: # The word 'javascript:' with any whitespace possible.
- .*? # Any characters (non-greedy) between the quotes.
- \1 # The previously captured single or double quote.
- """, re.U | re.S | re.I | re.X)
-
- _RE_EVENT_EXPR = (
- ur"""
- \b # The beginning of a new word.
- on\w+? # All words starting with 'on' (non-greedy)
- # example: |onmouseover|.
- \s*=\s* # The '=' with all whitespace before and after it.
- (?P<quote>[\'\"]) # A captured single or double quote.
- .*? # Any characters (non-greedy) between the quotes.
- \1 # The previously captured single or double quote.
- """)
-
- # This pattern is used for removing code with js events, such as |onload|.
- # By adding the leading |ur'<[^<>]*?'| and the trailing |'ur'[^<>]*?>'| the
- # pattern matches to strings such as '<tr class="nav"
- # onmouseover="mOvr1(this);" onmouseout="mOut1(this);">'
- _RE_TAG_WITH_EVENTS_PATTERN = re.compile(
- ur"""
- < # Matches character '<'.
- [^<>]*? # Matches any characters except '<' and '>' (non-greedy).""" +
- _RE_EVENT_EXPR +
- ur"""
- [^<>]*? # Matches any characters except '<' and '>' (non-greedy).
- > # Matches character '>'.
- """, re.U | re.S | re.I | re.X)
-
- # Adds whitespace chars at the end of the matched event. Also match trailing
- # whitespaces for JS events. Do not match leading whitespace.
- # For example: |< /form>| is invalid HTML and does not exist but |</form >| is
- # considered valid HTML.
- _RE_EVENT_PATTERN = re.compile(
- _RE_EVENT_EXPR + ur'\s*', re.U | re.S | re.I | re.X)
-
- # This pattern is used for finding form elements.
- _RE_FORM_PATTERN = re.compile(
- ur"""
- <form # A new opening '<form' tag.
- \b # The end of the word 'form'.
- .*? # Any characters (non-greedy).
- > # Ending of the (opening) tag: '>'.
- .*? # Any characters (non-greedy) between the tags.
- </form\s*> # The '</form>' closing tag.
- """, re.U | re.S | re.I | re.X)
-
- def __init__(self, input_dir=_REGISTRATION_PAGES_DIR,
- output_dir=_EXTRACTED_FORMS_DIR, logging_level=None):
- """Creates a FormsExtractor object.
-
- Args:
- input_dir: the directory of HTML files.
- output_dir: the directory where the registration form files will be
- saved.
- logging_level: verbosity level, default is None.
-
- Raises:
- IOError exception if input directory doesn't exist.
- """
- if logging_level:
- if not self.log_handlers['StreamHandler']:
- console = logging.StreamHandler()
- console.setLevel(logging.DEBUG)
- self.log_handlers['StreamHandler'] = console
- self.logger.addHandler(console)
- self.logger.setLevel(logging_level)
- else:
- if self.log_handlers['StreamHandler']:
- self.logger.removeHandler(self.log_handlers['StreamHandler'])
- self.log_handlers['StreamHandler'] = None
-
- self._input_dir = input_dir
- self._output_dir = output_dir
- if not os.path.isdir(self._input_dir):
- error_msg = 'Directory "%s" doesn\'t exist.' % self._input_dir
- self.logger.error('Error: %s', error_msg)
- raise IOError(error_msg)
- if not os.path.isdir(output_dir):
- os.makedirs(output_dir)
- self._form_location_comment = ''
-
- def _SubstituteAllEvents(self, matchobj):
- """Remove all js events that are present as attributes within a tag.
-
- Args:
- matchobj: A regexp |re.MatchObject| containing text that has at least one
- event. Example: |<tr class="nav" onmouseover="mOvr1(this);"
- onmouseout="mOut1(this);">|.
-
- Returns:
- The text containing the tag with all the attributes except for the tags
- with events. Example: |<tr class="nav">|.
- """
- tag_with_all_attrs = matchobj.group(0)
- return self._RE_EVENT_PATTERN.sub('', tag_with_all_attrs)
-
- def Extract(self, strip_js_only):
- """Extracts and saves the extracted registration forms.
-
- Iterates through all the HTML files.
-
- Args:
- strip_js_only: If True, only Javascript is stripped from the HTML content.
- Otherwise, all non-form elements are stripped.
- """
- pathname_pattern = os.path.join(self._input_dir, self._HTML_FILES_PATTERN)
- html_files = [f for f in glob.glob(pathname_pattern) if os.path.isfile(f)]
- for filename in html_files:
- self.logger.info('Stripping file "%s" ...', filename)
- with open(filename, 'U') as f:
- html_content = self._RE_TAG_WITH_EVENTS_PATTERN.sub(
- self._SubstituteAllEvents,
- self._RE_HREF_JS_PATTERN.sub(
- '', self._RE_SCRIPT_PATTERN.sub('', f.read())))
-
- form_filename = os.path.split(filename)[1] # Path dropped.
- form_filename = form_filename.replace(self._HTML_FILE_PREFIX, '', 1)
- (form_filename, extension) = os.path.splitext(form_filename)
- form_filename = (self._FORM_FILE_PREFIX + form_filename +
- '%s' + extension)
- form_filename = os.path.join(self._output_dir, form_filename)
- if strip_js_only:
- form_filename = form_filename % ''
- try:
- with open(form_filename, 'w') as f:
- f.write(html_content)
- except IOError as e:
- self.logger.error('Error: %s', e)
- continue
- else: # Remove all non form elements.
- match = self._RE_FORM_LOCATION_PATTERN.search(html_content)
- if match:
- form_location_comment = match.group() + os.linesep
- else:
- form_location_comment = ''
- forms_iterator = self._RE_FORM_PATTERN.finditer(html_content)
- for form_number, form_match in enumerate(forms_iterator, start=1):
- form_content = form_match.group()
- numbered_form_filename = form_filename % form_number
- try:
- with open(numbered_form_filename, 'w') as f:
- f.write(form_location_comment)
- f.write(form_content)
- except IOError as e:
- self.logger.error('Error: %s', e)
- continue
- self.logger.info('\tFile "%s" extracted SUCCESSFULLY!', filename)
-
-
-def main():
- # Command line options.
- parser = OptionParser()
- parser.add_option(
- '-l', '--log_level', metavar='LOG_LEVEL', default='error',
- help='LOG_LEVEL: debug, info, warning or error [default: %default]')
- parser.add_option(
- '-j', '--js', dest='js', action='store_true', default=False,
- help='Removes all javascript elements [default: %default]')
-
- (options, args) = parser.parse_args()
- options.log_level = options.log_level.upper()
- if options.log_level not in ['DEBUG', 'INFO', 'WARNING', 'ERROR']:
- print 'Wrong log_level argument.'
- parser.print_help()
- sys.exit(1)
-
- options.log_level = getattr(logging, options.log_level)
- extractor = FormsExtractor(logging_level=options.log_level)
- extractor.Extract(options.js)
-
-
-if __name__ == '__main__':
- main()
+#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + + +"""Extracts registration forms from the corresponding HTML files. + +Used for extracting forms within HTML files. This script is used in +conjunction with the webforms_aggregator.py script, which aggregates web pages +with fillable forms (i.e registration forms). + +The purpose of this script is to extract out all non-form elements that may be +causing parsing errors and timeout issues when running browser_tests. + +This script extracts all forms from a HTML file. +If there are multiple forms per downloaded site, multiple files are created +for each form. + +Used as a standalone script but assumes that it is run from the directory in +which it is checked into. + +Usage: forms_extractor.py [options] + +Options: + -l LOG_LEVEL, --log_level=LOG_LEVEL, + LOG_LEVEL: debug, info, warning or error [default: error] + -j, --js extracts javascript elements from web form. + -h, --help show this help message and exit +""" + +import glob +import logging +from optparse import OptionParser +import os +import re +import sys + + +class FormsExtractor(object): + """Extracts HTML files, leaving only registration forms from the HTML file.""" + _HTML_FILES_PATTERN = r'*.html' + _HTML_FILE_PREFIX = r'grabber-' + _FORM_FILE_PREFIX = r'grabber-stripped-' + + _REGISTRATION_PAGES_DIR = os.path.join(os.pardir, 'test', 'data', 'autofill', + 'heuristics', 'input') + _EXTRACTED_FORMS_DIR = os.path.join(os.pardir, 'test', 'data', 'autofill', + 'heuristics', 'input') + + logger = logging.getLogger(__name__) + log_handlers = {'StreamHandler': None} + + # This pattern is used for retrieving the form location comment located at the + # top of each downloaded HTML file indicating where the form originated from. + _RE_FORM_LOCATION_PATTERN = re.compile( + ur""" + <!--Form\s{1}Location: # Starting of form location comment. + .*? # Any characters (non-greedy). + --> # Ending of the form comment. + """, re.U | re.S | re.I | re.X) + + # This pattern is used for removing all script code. + _RE_SCRIPT_PATTERN = re.compile( + ur""" + <script # A new opening '<script' tag. + \b # The end of the word 'script'. + .*? # Any characters (non-greedy). + > # Ending of the (opening) tag: '>'. + .*? # Any characters (non-greedy) between the tags. + </script\s*> # The '</script>' closing tag. + """, re.U | re.S | re.I | re.X) + + # This pattern is used for removing all href js code. + _RE_HREF_JS_PATTERN = re.compile( + ur""" + \bhref # The word href and its beginning. + \s*=\s* # The '=' with all whitespace before and after it. + (?P<quote>[\'\"]) # A single or double quote which is captured. + \s*javascript\s*: # The word 'javascript:' with any whitespace possible. + .*? # Any characters (non-greedy) between the quotes. + \1 # The previously captured single or double quote. + """, re.U | re.S | re.I | re.X) + + _RE_EVENT_EXPR = ( + ur""" + \b # The beginning of a new word. + on\w+? # All words starting with 'on' (non-greedy) + # example: |onmouseover|. + \s*=\s* # The '=' with all whitespace before and after it. + (?P<quote>[\'\"]) # A captured single or double quote. + .*? # Any characters (non-greedy) between the quotes. + \1 # The previously captured single or double quote. + """) + + # This pattern is used for removing code with js events, such as |onload|. + # By adding the leading |ur'<[^<>]*?'| and the trailing |'ur'[^<>]*?>'| the + # pattern matches to strings such as '<tr class="nav" + # onmouseover="mOvr1(this);" onmouseout="mOut1(this);">' + _RE_TAG_WITH_EVENTS_PATTERN = re.compile( + ur""" + < # Matches character '<'. + [^<>]*? # Matches any characters except '<' and '>' (non-greedy).""" + + _RE_EVENT_EXPR + + ur""" + [^<>]*? # Matches any characters except '<' and '>' (non-greedy). + > # Matches character '>'. + """, re.U | re.S | re.I | re.X) + + # Adds whitespace chars at the end of the matched event. Also match trailing + # whitespaces for JS events. Do not match leading whitespace. + # For example: |< /form>| is invalid HTML and does not exist but |</form >| is + # considered valid HTML. + _RE_EVENT_PATTERN = re.compile( + _RE_EVENT_EXPR + ur'\s*', re.U | re.S | re.I | re.X) + + # This pattern is used for finding form elements. + _RE_FORM_PATTERN = re.compile( + ur""" + <form # A new opening '<form' tag. + \b # The end of the word 'form'. + .*? # Any characters (non-greedy). + > # Ending of the (opening) tag: '>'. + .*? # Any characters (non-greedy) between the tags. + </form\s*> # The '</form>' closing tag. + """, re.U | re.S | re.I | re.X) + + def __init__(self, input_dir=_REGISTRATION_PAGES_DIR, + output_dir=_EXTRACTED_FORMS_DIR, logging_level=None): + """Creates a FormsExtractor object. + + Args: + input_dir: the directory of HTML files. + output_dir: the directory where the registration form files will be + saved. + logging_level: verbosity level, default is None. + + Raises: + IOError exception if input directory doesn't exist. + """ + if logging_level: + if not self.log_handlers['StreamHandler']: + console = logging.StreamHandler() + console.setLevel(logging.DEBUG) + self.log_handlers['StreamHandler'] = console + self.logger.addHandler(console) + self.logger.setLevel(logging_level) + else: + if self.log_handlers['StreamHandler']: + self.logger.removeHandler(self.log_handlers['StreamHandler']) + self.log_handlers['StreamHandler'] = None + + self._input_dir = input_dir + self._output_dir = output_dir + if not os.path.isdir(self._input_dir): + error_msg = 'Directory "%s" doesn\'t exist.' % self._input_dir + self.logger.error('Error: %s', error_msg) + raise IOError(error_msg) + if not os.path.isdir(output_dir): + os.makedirs(output_dir) + self._form_location_comment = '' + + def _SubstituteAllEvents(self, matchobj): + """Remove all js events that are present as attributes within a tag. + + Args: + matchobj: A regexp |re.MatchObject| containing text that has at least one + event. Example: |<tr class="nav" onmouseover="mOvr1(this);" + onmouseout="mOut1(this);">|. + + Returns: + The text containing the tag with all the attributes except for the tags + with events. Example: |<tr class="nav">|. + """ + tag_with_all_attrs = matchobj.group(0) + return self._RE_EVENT_PATTERN.sub('', tag_with_all_attrs) + + def Extract(self, strip_js_only): + """Extracts and saves the extracted registration forms. + + Iterates through all the HTML files. + + Args: + strip_js_only: If True, only Javascript is stripped from the HTML content. + Otherwise, all non-form elements are stripped. + """ + pathname_pattern = os.path.join(self._input_dir, self._HTML_FILES_PATTERN) + html_files = [f for f in glob.glob(pathname_pattern) if os.path.isfile(f)] + for filename in html_files: + self.logger.info('Stripping file "%s" ...', filename) + with open(filename, 'U') as f: + html_content = self._RE_TAG_WITH_EVENTS_PATTERN.sub( + self._SubstituteAllEvents, + self._RE_HREF_JS_PATTERN.sub( + '', self._RE_SCRIPT_PATTERN.sub('', f.read()))) + + form_filename = os.path.split(filename)[1] # Path dropped. + form_filename = form_filename.replace(self._HTML_FILE_PREFIX, '', 1) + (form_filename, extension) = os.path.splitext(form_filename) + form_filename = (self._FORM_FILE_PREFIX + form_filename + + '%s' + extension) + form_filename = os.path.join(self._output_dir, form_filename) + if strip_js_only: + form_filename = form_filename % '' + try: + with open(form_filename, 'w') as f: + f.write(html_content) + except IOError as e: + self.logger.error('Error: %s', e) + continue + else: # Remove all non form elements. + match = self._RE_FORM_LOCATION_PATTERN.search(html_content) + if match: + form_location_comment = match.group() + os.linesep + else: + form_location_comment = '' + forms_iterator = self._RE_FORM_PATTERN.finditer(html_content) + for form_number, form_match in enumerate(forms_iterator, start=1): + form_content = form_match.group() + numbered_form_filename = form_filename % form_number + try: + with open(numbered_form_filename, 'w') as f: + f.write(form_location_comment) + f.write(form_content) + except IOError as e: + self.logger.error('Error: %s', e) + continue + self.logger.info('\tFile "%s" extracted SUCCESSFULLY!', filename) + + +def main(): + parser = OptionParser() + parser.add_option( + '-l', '--log_level', metavar='LOG_LEVEL', default='error', + help='LOG_LEVEL: debug, info, warning or error [default: %default]') + parser.add_option( + '-j', '--js', dest='js', action='store_true', default=False, + help='Removes all javascript elements [default: %default]') + + (options, args) = parser.parse_args() + options.log_level = options.log_level.upper() + if options.log_level not in ['DEBUG', 'INFO', 'WARNING', 'ERROR']: + print 'Wrong log_level argument.' + parser.print_help() + return 1 + + options.log_level = getattr(logging, options.log_level) + extractor = FormsExtractor(logging_level=options.log_level) + extractor.Extract(options.js) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) |