def interactively_edit(self): r'''Interactively edits file. Returns none. ''' command = 'vim + {}'.format(self.filesystem_path) iotools.spawn_subprocess(command)
def _interactively_view_versioned_file(self, extension): assert extension in ('.ly', '.pdf', '.py') getter = self.session.io_manager.make_getter(where=self._where) last_version_number = self._get_last_version_number() if last_version_number is None: message = 'versions directory empty.' self.session.io_manager.proceed(message) return prompt = 'version number (0-{})' prompt = prompt.format(last_version_number) getter.append_integer(prompt) version_number = getter._run(clear_terminal=False) if self.session.backtrack(): return if last_version_number < version_number or \ (version_number < 0 and last_version_number < abs(version_number)): message = "version {} doesn't exist yet." message = message.format(version_number) self.session.io_manager.proceed(['', message]) if version_number < 0: version_number = last_version_number + version_number + 1 version_string = str(version_number).zfill(4) file_name = '{}{}'.format(version_string, extension) file_path = os.path.join( self.filesystem_path, 'versions', file_name, ) if os.path.isfile(file_path): if extension in ('.ly', '.py'): command = 'vim -R {}'.format(file_path) elif extension == '.pdf': command = 'open {}'.format(file_path) iotools.spawn_subprocess(command)
def process_args(self, args): from abjad import abjad_configuration commit_file = 'abjad_commit.txt' commit_path = os.path.join( abjad_configuration.abjad_configuration_directory_path, commit_file) while not os.path.exists(commit_path): SvnMessageScript()() commit_command = 'svn commit {} -F {}'.format(args.path, commit_path) while True: print '\nSTORED COMMIT MESSAGE:\n' with open(commit_path, 'r') as f: lines = f.read().splitlines() for line in lines: print '~ {}'.format(line) print result = raw_input( 'Accept [Y], Reject [n], Abort [a]: ').strip().lower() if result in ('', 'y', 'yes'): iotools.spawn_subprocess(commit_command) return elif result in ('n', 'no'): SvnMessageScript()() elif result in ('a', 'abort'): return else: print 'Invalid response {!r}, repeating...'.format(result)
def log(): r'''Opens the LilyPond log file in operating system-specific text editor. :: >>> iotools.log() # doctest: +SKIP :: GNU LilyPond 2.12.2 Processing `0440.ly' Parsing... Interpreting music... Preprocessing graphical objects... Finding the ideal number of pages... Fitting music on 1 page... Drawing systems... Layout output to `0440.ps'... Converting to `./0440.pdf'... Returns none. ''' from abjad import abjad_configuration from abjad.tools import iotools abjad_output = abjad_configuration['abjad_output'] text_editor = abjad_configuration.get_text_editor() log_file_path = os.path.join(abjad_output, 'lily.log') command = '{} {}'.format(text_editor, log_file_path) iotools.spawn_subprocess(command)
def process_args(self, args): from abjad import abjad_configuration text_editor = abjad_configuration.get_text_editor() if args.clean: if os.path.exists(self.commit_message_path): os.remove(self.commit_message_path) command = '{} {}'.format(text_editor, self.commit_message_path) iotools.spawn_subprocess(command)
def interactively_call_lilypond(self, prompt=True): r'''Interactively calls LilyPond on file. Returns none. ''' command = 'lily {}'.format(self.filesystem_path) iotools.spawn_subprocess(command) self.session.io_manager.proceed('', is_interactive=prompt)
def view_output_pdf(self): r'''Views output PDF. Returns none. ''' output_pdf_file_path = self._get_output_pdf_file_path() if os.path.isfile(output_pdf_file_path): command = 'open {}'.format(output_pdf_file_path) iotools.spawn_subprocess(command)
def interactively_view_current_output_ly(self): r'''Interactively views current output LilyPond file. Returns none. ''' output_lilypond_file_path = self._get_output_lilypond_file_path() if os.path.isfile(output_lilypond_file_path): command = 'vim -R {}'.format(output_lilypond_file_path) iotools.spawn_subprocess(command)
def process_args(self, args): if args.clean: clean_args = [args.path] clean_script = CleanScript() clean_script(clean_args) print 'Updating...' iotools.spawn_subprocess('svn update {}'.format(args.path))
def interactively_view(self): r'''Interactively views file. Returns none. ''' if self.filesystem_path.endswith('.pdf'): command = 'open {}'.format(self.filesystem_path) else: command = 'vim -R {}'.format(self.filesystem_path) iotools.spawn_subprocess(command)
def process_args(self, args): iotools.clear_terminal() if args.whole_words_only: whole_words_only = '-w' else: whole_words_only = '' command = r'grep {} -Irn {!r} {} | grep -v svn-base | grep -v svn\/ | grep -v docs'.format( whole_words_only, args.pattern, os.path.relpath(args.path)) iotools.spawn_subprocess(command)
def process_args(self, args): command = 'svn st {}'.format(args.path) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) lines = process.stdout.readlines() for line in lines: if line.startswith('?'): command = 'svn add {}'.format(line.split()[-1]) iotools.spawn_subprocess(command)
def interactively_edit_calling_code(self): if self.where is not None: file_name = self.where[1] line_number = self.where[2] command = 'vim +{} {}'.format(line_number, file_name) iotools.spawn_subprocess(command) else: lines = [] message = 'where-tracking not enabled.' message += " Use 'twt' to toggle where-tracking." lines.append(message) lines.append('') self.session.io_manager.display(lines) self.session.hide_next_redraw = True
def clear_terminal(): '''Runs ``clear`` if OS is POSIX-compliant (UNIX / Linux / MacOS). Runs ``cls`` if OS is not POSIX-compliant (Windows). :: >>> iotools.clear_terminal() # doctest: +SKIP Returns none. ''' from abjad.tools import iotools if os.name == 'posix': command = 'clear' else: command = 'cls' iotools.spawn_subprocess(command)
def run_lilypond(lilypond_file_name, lilypond_path): r'''Runs LilyPond. Returns none. ''' from abjad import abjad_configuration from abjad.tools import iotools abjad_output_directory_path = abjad_configuration['abjad_output'] if not lilypond_path: lilypond_path = 'lilypond' log_file_path = os.path.join(abjad_output_directory_path, 'lily.log') command = '{} -dno-point-and-click {} > {} 2>&1' command = command.format(lilypond_path, lilypond_file_name, log_file_path) iotools.spawn_subprocess(command) postscript_file_name = lilypond_file_name.replace('.ly', '.ps') try: os.remove(postscript_file_name) except OSError: # no such file... pass
def open_file(file_path, application=None): r'''Opens `file_path` with operating system-specific file-opener with `application` is none. Opens `file_path` with `application` when `application` is not none. Returns none. ''' from abjad.tools import iotools if os.name == 'nt': os.startfile(file_path) return if sys.platform.lower() == 'linux2': viewer = application or 'xdg-open' else: viewer = application or 'open' command = '{} {} &'.format(viewer, file_path) iotools.spawn_subprocess(command)
def run_abjad(): r'''Runs Abjad. Returns none. ''' from abjad.tools import iotools try: file = sys.argv[1] except IndexError: file = '' commands = [ "from abjad import *;", "print abjad_configuration.get_abjad_startup_string();", ] command = r'''python -i {} -c "{}"'''.format(file, ' '.join(commands)) iotools.spawn_subprocess(command)
def _rename_old_test_files(self, kind, old_codebase, old_package_name, old_object_name, new_codebase, new_package_name, new_object_name): print 'Renaming old test file(s) ...' old_tools_path = self._codebase_name_to_codebase_tools_path( old_codebase) new_tools_path = self._codebase_name_to_codebase_tools_path( new_codebase) if kind == 'function': old_test_file = 'test_{}_{}.py'.format( old_package_name, old_object_name) old_path = os.path.join( old_tools_path, old_package_name, 'test') old_path = os.path.join(old_path, old_test_file) new_test_file = 'test_{}_{}.py'.format( new_package_name, new_object_name) new_path = os.path.join(new_tools_path, new_package_name, 'test') new_path = os.path.join(new_path, new_test_file) command = 'svn --force mv {} {} > /dev/null'.format( old_path, new_path) iotools.spawn_subprocess(command) elif kind == 'class': test_path = os.path.join( new_tools_path, new_package_name, new_object_name, 'test') for x in os.listdir(test_path): if x.startswith('test_') and x.endswith('.py'): old_path = os.path.join(test_path, x) new_path = os.path.join(test_path, x.replace( 'test_{}'.format(old_object_name), 'test_{}'.format(new_object_name))) command = 'svn --force mv {} {} > /dev/null'.format( old_path, new_path) iotools.spawn_subprocess(command) print ''
def _build_mainline_api(self, format='html', clean=False): from abjad import abjad_configuration AbjadAPIGenerator()(verbose=True) # print greeting print 'Now building the {} docs ...'.format(format.upper()) print '' # change to docs directory because makefile lives there docs_directory = os.path.relpath(os.path.join( abjad_configuration.abjad_directory_path, 'docs')) os.chdir(docs_directory) # optionally, make clean before building if clean: print 'Cleaning build directory ...' iotools.spawn_subprocess('make clean') if format == 'coverage': iotools.spawn_subprocess('sphinx-build -b coverage {} {}'.format( 'source', os.path.join('build', 'coverage'), )) else: iotools.spawn_subprocess('make {}'.format(format))
def interactively_view_asset_pdfs( self, pending_user_input=None, ): self.session.io_manager.assign_user_input(pending_user_input) parts = (self.session.current_score_directory_path,) parts += self.score_package_asset_storehouse_path_infix_parts segments_directory_path = os.path.join(*parts) output_pdf_file_paths = [] for directory_entry in os.listdir(segments_directory_path): if not directory_entry[0].isalpha(): continue segment_package_name = directory_entry output_pdf_file_path = os.path.join( segments_directory_path, segment_package_name, 'output.pdf', ) if os.path.isfile(output_pdf_file_path): output_pdf_file_paths.append(output_pdf_file_path) command = ' '.join(output_pdf_file_paths) command = 'open ' + command iotools.spawn_subprocess(command)
def interactively_view_all_versioned_pdfs(self): r'''Interactively views all versioend PDFs. Returns none. ''' versions_directory_path = self._get_versions_directory_path() file_paths = [] for directory_entry in os.listdir(versions_directory_path): if not directory_entry[0].isdigit(): continue if not directory_entry.endswith('.pdf'): continue file_path = os.path.join( versions_directory_path, directory_entry, ) file_paths.append(file_path) if not file_paths: message = 'version directory empty.' self.session.io_manager.proceed(message) return file_paths = ' '.join(file_paths) command = 'open {}'.format(file_paths) iotools.spawn_subprocess(command)
def _rename_old_module(self, kind, old_codebase, old_package_name, old_object_name, new_codebase, new_package_name, new_object_name): print 'Renaming old module ...' old_tools_path = self._codebase_name_to_codebase_tools_path( old_codebase) new_tools_path = self._codebase_name_to_codebase_tools_path( new_codebase) if kind == 'function': old_module = old_object_name + '.py' old_path = os.path.join( old_tools_path, old_package_name, old_module) new_module = new_object_name + '.py' new_path = os.path.join( new_tools_path, new_package_name, new_module) command = 'svn --force mv {} {} > /dev/null'.format( old_path, new_path) iotools.spawn_subprocess(command) elif kind == 'class': # move the folder... old_class_package = os.path.join( old_tools_path, old_package_name, old_object_name) new_class_package = os.path.join( new_tools_path, new_package_name, new_object_name) command = 'svn --force mv {} {} > /dev/null'.format( old_class_package, new_class_package) iotools.spawn_subprocess(command) # ...then the file old_class_module = os.path.join( new_class_package, '{}.py'.format(old_object_name)) new_class_module = os.path.join( new_class_package, '{}.py'.format(new_object_name)) command = 'svn --force mv {} {} > /dev/null'.format( old_class_module, new_class_module) iotools.spawn_subprocess(command) print ''
def _rename_old_api_page(self, kind, old_codebase, old_package_name, old_object_name, new_codebase, new_package_name, new_object_name): print 'Renaming old API page ...' old_docs_path = self._codebase_name_to_codebase_docs_path(old_codebase) new_docs_path = self._codebase_name_to_codebase_docs_path(new_codebase) old_rst_file_name = old_object_name + '.rst' new_rst_file_name = new_object_name + '.rst' if kind == 'function': old_api_path = os.path.join( old_docs_path, old_package_name, old_rst_file_name) new_api_path = os.path.join( new_docs_path, new_package_name, new_rst_file_name) command = 'svn --force mv {} {} > /dev/null'.format( old_api_path, new_api_path) iotools.spawn_subprocess(command) elif kind == 'class': # move the folder... old_api_path = os.path.join( old_docs_path, old_package_name, old_object_name) new_api_path = os.path.join( new_docs_path, new_package_name, new_object_name) command = 'svn --force mv {} {} > /dev/null'.format( old_api_path, new_api_path) iotools.spawn_subprocess(command) # ...then the file old_rst_file_name = os.path.join(new_api_path, old_rst_file_name) new_rst_file_name = os.path.join(new_api_path, new_rst_file_name) command = 'svn --force mv {} {} > /dev/null'.format( old_rst_file_name, new_rst_file_name) iotools.spawn_subprocess(command) print ''
def interactively_typeset_tex_file(self, prompt=True): r'''Interactively typesets TeX file. Returns none. ''' input_directory = os.path.dirname(self.filesystem_path) basename = os.path.basename(self.filesystem_path) input_file_name_stem, extension = os.path.splitext(basename) output_directory = input_directory command = 'pdflatex --jobname={} -output-directory={} {}/{}.tex' command = command.format( input_file_name_stem, output_directory, input_directory, input_file_name_stem, ) iotools.spawn_subprocess(command) command = 'rm {}/*.aux'.format(output_directory) iotools.spawn_subprocess(command) command = 'rm {}/*.log'.format(output_directory) iotools.spawn_subprocess(command) self.session.io_manager.proceed('', is_interactive=prompt)
def _build_experimental_api(self, format='html', clean=False): from abjad import abjad_configuration class ExperimentalAPIGenerator(AbjadAPIGenerator): _api_title = 'Abjad Experimental API' @property def docs_api_index_path(self): return os.path.join( abjad_configuration.abjad_experimental_directory_path, 'docs', 'source', 'index.rst') @property def path_definitions(self): from abjad import abjad_configuration return ( ( os.path.join( abjad_configuration.abjad_experimental_directory_path, 'tools'), os.path.join( abjad_configuration.abjad_experimental_directory_path, 'docs', 'source', 'tools'), 'experimental.tools.', ), ( os.path.join( abjad_configuration.abjad_experimental_directory_path, 'demos'), os.path.join( abjad_configuration.abjad_experimental_directory_path, 'docs', 'source', 'demos'), 'experimental.demos.', ), ) @property def root_package(self): return 'experimental' @property def tools_package_path_index(self): return 2 ExperimentalAPIGenerator()(verbose=True) # print greeting print 'Now building the Experimental {} docs ...'.format( format.upper()) print '' # change to docs directory because makefile lives there docs_directory = os.path.join( abjad_configuration.abjad_experimental_directory_path, 'docs') os.chdir(docs_directory) # optionally, make clean before building if clean: print 'Cleaning build directory ...' iotools.spawn_subprocess('make clean') if format == 'coverage': iotools.spawn_subprocess('sphinx-build -b coverage {} {}'.format( 'source', os.path.join('build', 'coverage'), )) else: iotools.spawn_subprocess('make {}'.format(format))
def interactively_edit(self): columns = len(self.material_package_name) + 3 command = "vim + -c'norm {}l' {}" command = command.format(columns, self.filesystem_path) iotools.spawn_subprocess(command)
def interactively_save_to_versions_directory( self, is_interactive=True ): r'''Interactively saves asset definition module, output PDF and output LilyPond file to versions directory. Returns none. ''' paths = {} asset_definition_module_file_path = \ self._get_asset_definition_module_file_path() if not os.path.isfile(asset_definition_module_file_path): message = 'can not find asset definition module.' self.session.io_manager.proceed( message, is_interactive=is_interactive, ) return output_pdf_file_path = self._get_output_pdf_file_path() if not os.path.isfile(output_pdf_file_path): message = 'can not find output PDF.' self.session.io_manager.proceed( message, is_interactive=is_interactive, ) return output_lilypond_file_path = self._get_output_lilypond_file_path() if not os.path.isfile(output_lilypond_file_path): message = 'can not find output LilyPond file.' self.session.io_manager.proceed( message, is_interactive=is_interactive, ) return next_output_file_name = iotools.get_next_output_file_name( output_directory_path=self._get_versions_directory_path(), ) result = os.path.splitext(next_output_file_name) next_output_file_name_root, extension = result target_file_name = next_output_file_name_root + '.py' target_file_path = os.path.join( self._get_versions_directory_path(), target_file_name, ) command = 'cp {} {}'.format( asset_definition_module_file_path, target_file_path, ) iotools.spawn_subprocess(command) target_file_name = next_output_file_name_root + '.pdf' target_file_path = os.path.join( self._get_versions_directory_path(), target_file_name, ) command = 'cp {} {}'.format( output_pdf_file_path, target_file_path, ) iotools.spawn_subprocess(command) target_file_name = next_output_file_name_root + '.ly' target_file_path = os.path.join( self._get_versions_directory_path(), target_file_name, ) command = 'cp {} {}'.format( output_lilypond_file_path, target_file_path, ) iotools.spawn_subprocess(command) version_number = int(next_output_file_name_root) message = 'version {} written to disk.' message = message.format(version_number) self.session.io_manager.proceed( message, is_interactive=is_interactive, ) return version_number
def _run_python(self, prompt=True): command = 'python {}'.format(self.filesystem_path) iotools.spawn_subprocess(command) message = 'file executed.' self.session.io_manager.proceed(message, is_interactive=prompt)
def interactively_rename_package(self): base_name = os.path.basename(self.filesystem_path) line = 'current name: {}'.format(base_name) self.session.io_manager.display(line) getter = self.session.io_manager.make_getter(where=self._where) getter.append_snake_case_package_name('new name') new_package_name = getter._run() if self.session.backtrack(): return lines = [] lines.append('current name: {}'.format(base_name)) lines.append('new name: {}'.format(new_package_name)) lines.append('') self.session.io_manager.display(lines) if not self.session.io_manager.confirm(): return new_directory_path = self.filesystem_path.replace( base_name, new_package_name, ) if self._is_versioned(): # rename package command = 'svn mv {} {}' command = command.format(self.filesystem_path, new_directory_path) iotools.spawn_subprocess(command) # replace output material variable name new_output_material_module_name = os.path.join( new_directory_path, 'output_material.py', ) result = os.path.splitext(base_name) old_package_name, extension = result self.replace_in_file( new_output_material_module_name, old_package_name, new_package_name, ) # commit commit_message = 'renamed {} to {}.' commit_message = commit_message.format( base_name, new_package_name, ) commit_message = commit_message.replace('_', ' ') parent_directory_path = os.path.dirname(self.filesystem_path) command = 'svn commit -m "{}" {}' command = command.format( commit_message, parent_directory_path, ) iotools.spawn_subprocess(command) else: # rename package command = 'mv {} {}' command = command.format(self.filesystem_path, new_directory_path) iotools.spawn_subprocess(command) # replace output material variable name new_output_material_module_name = os.path.join( new_directory_path, 'output_material.py', ) result = os.path.splitext(base_name) old_package_name, extension = result self.replace_in_file( new_output_material_module_name, old_package_name, new_package_name, ) # update path name to reflect change self._path = new_directory_path self.session.is_backtracking_locally = True
def _interpret_in_external_process(self): command = 'python {}'.format(self.filesystem_path) result = iotools.spawn_subprocess(command) if result != 0: self.session.io_manager.display('') self.session.io_manager.proceed()