
import sys
import os.path
import re

INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"')

seen_files = set()

def add_file(filename):
  basename = os.path.basename(filename)
  # Only include each file at most once.
  if basename in seen_files:
    return
  seen_files.add(basename)
  path = os.path.dirname(filename)

  with open(filename, 'r') as f:
    for line in f:
      m = INCLUDE_PATTERN.match(line)
      if m:
        add_file(os.path.join(path, m.group(1)))
      else:
        sys.stdout.write(line)

for f in sys.argv[1:]:
  add_file(f)


import sys
from os.path import basename, dirname, join
import re

INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"')

seen_files = set()
out = sys.stdout

def add_file(filename):
  bname = basename(filename)
  # Only include each file at most once.
  if bname in seen_files:
    return
  seen_files.add(bname)
  path = dirname(filename)

  out.write('// Begin file "{0}"\n'.format(filename))
  with open(filename, 'r') as f:
    for line in f:
      m = INCLUDE_PATTERN.match(line)
      if m:
        add_file(join(path, m.group(1)))
      else:
        out.write(line)
  out.write('// End file "{0}"\n'.format(filename))

for f in sys.argv[1:]:
  add_file(f)

Add comments for file start/end
Add comments for file start/end

  
  import sys
- import os.path
+ from os.path import basename, dirname, join
  import re
  
  INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"')
  
  seen_files = set()
+ out = sys.stdout
  
  def add_file(filename):
-   basename = os.path.basename(filename)
+   bname = basename(filename)
    # Only include each file at most once.
-   if basename in seen_files:
+   if bname in seen_files:
      return
-   seen_files.add(basename)
+   seen_files.add(bname)
-   path = os.path.dirname(filename)
+   path = dirname(filename)
  
+   out.write('// Begin file "{0}"\n'.format(filename))
    with open(filename, 'r') as f:
      for line in f:
        m = INCLUDE_PATTERN.match(line)
        if m:
-         add_file(os.path.join(path, m.group(1)))
+         add_file(join(path, m.group(1)))
        else:
-         sys.stdout.write(line)
+         out.write(line)
+   out.write('// End file "{0}"\n'.format(filename))
  
  for f in sys.argv[1:]:
    add_file(f)
  
Add comments for file start/end
## Code Before:

import sys
import os.path
import re

INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"')

seen_files = set()

def add_file(filename):
  basename = os.path.basename(filename)
  # Only include each file at most once.
  if basename in seen_files:
    return
  seen_files.add(basename)
  path = os.path.dirname(filename)

  with open(filename, 'r') as f:
    for line in f:
      m = INCLUDE_PATTERN.match(line)
      if m:
        add_file(os.path.join(path, m.group(1)))
      else:
        sys.stdout.write(line)

for f in sys.argv[1:]:
  add_file(f)

## Instruction:
Add comments for file start/end
## Code After:

import sys
from os.path import basename, dirname, join
import re

INCLUDE_PATTERN = re.compile(r'^\s*#include "([\w.]+)"')

seen_files = set()
out = sys.stdout

def add_file(filename):
  bname = basename(filename)
  # Only include each file at most once.
  if bname in seen_files:
    return
  seen_files.add(bname)
  path = dirname(filename)

  out.write('// Begin file "{0}"\n'.format(filename))
  with open(filename, 'r') as f:
    for line in f:
      m = INCLUDE_PATTERN.match(line)
      if m:
        add_file(join(path, m.group(1)))
      else:
        out.write(line)
  out.write('// End file "{0}"\n'.format(filename))

for f in sys.argv[1:]:
  add_file(f)
