Friday, October 20, 2006

Backup de Programas en ABAP


Este pequeño programa, nos permite grabar el código fuente, includes y elementos de texto de cualquier programa -:) Es bastante simple, solamente nos pide el nombre del programa y una ruta donde guardarlo -;)


**************************************************************
* Programa : Z_PROGRAMS_DOWNLOAD. *
* Módulo : BC - Basis. *
* Consultor ABAP : Alvaro Tejada Galindo. *
* February 14, 2006 12:33:22 PM *
**************************************************************

REPORT Z_DUMMY_ATG NO STANDARD PAGE HEADING.

*=============================================================
* VARIABLES
*=============================================================
DATA: FULLNAME(30) TYPE C,
ONE LIKE PCFILE-DRIVE,
TWO LIKE PCFILE-PATH,
FILEPATH(128) TYPE C.

DATA: APP_NAME TYPE STRING,
DDTEXT_NAME TYPE STRING,
DUMMY TYPE STRING,
INCLUDE_NAME TYPE STRING.

*=============================================================
* TABLAS INTERNAS
*=============================================================
DATA: BEGIN OF SOURCE_TABLE OCCURS 0,
LINEA(150) TYPE C.
DATA: END OF SOURCE_TABLE.

DATA: BEGIN OF INCLUDE_TABLE OCCURS 0,
LINEA(150) TYPE C.
DATA: END OF INCLUDE_TABLE.

DATA: BEGIN OF DYNPRO_TABLE OCCURS 0,
LINEA(250) TYPE C.
DATA: END OF DYNPRO_TABLE.

DATA: BEGIN OF T_TRDIR OCCURS 0,
NAME LIKE TRDIR-NAME,
END OF T_TRDIR.

DATA: BEGIN OF T_LOG OCCURS 0,
ERROR TYPE STRING,
END OF T_LOG.

DATA: DATA_DDTEXT TYPE STANDARD TABLE OF TEXTPOOL
WITH HEADER LINE.

*=============================================================
* SELECTION-SCREEN
*=============================================================
SELECTION-SCREEN BEGIN OF BLOCK TEST WITH FRAME.
PARAMETERS:
PRGNAME LIKE TRDIR-NAME,
LISTNAME LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK TEST.

*=============================================================
* AT SELECTION-SCREEN
*=============================================================
AT SELECTION-SCREEN ON VALUE-REQUEST FOR LISTNAME.
PERFORM GET_FILENAME CHANGING LISTNAME.

*=============================================================
* START-OF-SELECTION
*=============================================================
START-OF-SELECTION.
PERFORM LOAD_DATA.

IF NOT T_LOG[] IS INITIAL.
WRITE: 'Se encontraron los siguientes errores: '.
ENDIF.

ULINE.
SKIP 1.

LOOP AT T_LOG.
WRITE:/ T_LOG-ERROR.
ENDLOOP.

*-----------------------------------------------------------*
* FORM GET_FILENAME *
*-----------------------------------------------------------*
FORM GET_FILENAME CHANGING LISTNAME.

CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = LISTNAME
DEF_PATH = 'C:\downloads\lista'
MASK = ',*.*,*.*. '
MODE = 'S'
TITLE = 'Guardar en'
IMPORTING
FILENAME = LISTNAME
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.
ENDFORM. "GET_FILENAME

*-----------------------------------------------------------*
* FORM LOAD_DATA *
*-----------------------------------------------------------*
FORM LOAD_DATA.
DATA: W_FILE LIKE PCFILE-PATH.
W_FILE = LISTNAME.
CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'
EXPORTING
COMPLETE_FILENAME = W_FILE
IMPORTING
DRIVE = ONE
PATH = TWO
EXCEPTIONS
INVALID_DRIVE = 1
INVALID_EXTENSION = 2
INVALID_NAME = 3
INVALID_PATH = 4
OTHERS = 5.
CONCATENATE ONE ':' TWO INTO FILEPATH.

CONCATENATE PRGNAME '%' INTO FULLNAME.

SELECT NAME
INTO TABLE T_TRDIR
FROM TRDIR
WHERE NAME LIKE FULLNAME.

IF SY-SUBRC EQ 0.
PERFORM FILL_LIST.

PERFORM DOWNLOAD_APPS.
ENDIF.

ENDFORM. "LOAD_DATA

*-----------------------------------------------------------*
* FORM FILL_LIST *
*-----------------------------------------------------------*
FORM FILL_LIST.
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
FILENAME = FILEPATH
FILETYPE = 'ASC'
TABLES
DATA_TAB = T_TRDIR
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 02
INVALID_FILESIZE = 03
INVALID_TABLE_WIDTH = 04
INVALID_TYPE = 05
NO_BATCH = 06
UNKNOWN_ERROR = 07.
ENDFORM. "FILL_LIST

*-----------------------------------------------------------*
* FORM FILL_LIST *
*-----------------------------------------------------------*
FORM FILL_LIST_INCLUDE.

CONCATENATE FILEPATH 'Includes.txt' INTO LISTNAME.

CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
FILENAME = LISTNAME
FILETYPE = 'ASC'
MODE = 'A'
TABLES
DATA_TAB = INCLUDE_TABLE
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 02
INVALID_FILESIZE = 03
INVALID_TABLE_WIDTH = 04
INVALID_TYPE = 05
NO_BATCH = 06
UNKNOWN_ERROR = 07.
ENDFORM. "FILL_LIST_INCLUDE

*&----------------------------------------------------------*
*& Form DOWNLOAD_APPS *
*&----------------------------------------------------------*
FORM DOWNLOAD_APPS.

LOOP AT T_TRDIR.
CLEAR: APP_NAME, INCLUDE_NAME.

REFRESH: SOURCE_TABLE.
CLEAR: SOURCE_TABLE.

READ REPORT T_TRDIR-NAME INTO SOURCE_TABLE.
READ TEXTPOOL T_TRDIR-NAME INTO DATA_DDTEXT LANGUAGE SY-LANGU.

CONCATENATE FILEPATH T_TRDIR-NAME '.abp' INTO APP_NAME.
CONCATENATE FILEPATH T_TRDIR-NAME '_DDTEXT.txt'
INTO DDTEXT_NAME.

PERFORM DOWNLOAD_FORM USING APP_NAME.
PERFORM DOWNLOAD_DDTEXT USING DDTEXT_NAME.

LOOP AT SOURCE_TABLE.
SEARCH SOURCE_TABLE-LINEA FOR 'INCLUDE'.
IF SY-SUBRC EQ 0.

IF SOURCE_TABLE-LINEA+0(8) NE 'INCLUDE'.
CONTINUE.
ENDIF.

CLEAR INCLUDE_NAME.

SPLIT SOURCE_TABLE-LINEA AT SPACE
INTO DUMMY INCLUDE_NAME.
SPLIT INCLUDE_NAME AT '.'
INTO INCLUDE_NAME DUMMY.

REFRESH: SOURCE_TABLE.
CLEAR: SOURCE_TABLE.

MOVE INCLUDE_NAME TO INCLUDE_TABLE.
READ TABLE INCLUDE_TABLE WITH KEY LINEA = INCLUDE_TABLE.
IF SY-SUBRC NE 0.
APPEND INCLUDE_TABLE.
ENDIF.

READ REPORT INCLUDE_TABLE INTO SOURCE_TABLE.

IF SY-SUBRC EQ 0.
CONCATENATE FILEPATH INCLUDE_NAME '.abp'
INTO INCLUDE_NAME.
PERFORM DOWNLOAD_FORM USING INCLUDE_NAME.
PERFORM FILL_LIST_INCLUDE.
ENDIF.

ENDIF.
ENDLOOP.

ENDLOOP.

ENDFORM. "DOWNLOAD_APPS

*-----------------------------------------------------------*
* FORM DOWNLOAD_FORM *
*-----------------------------------------------------------*
FORM DOWNLOAD_FORM USING APP_NAME.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = APP_NAME
FILETYPE = 'ASC'
TABLES
DATA_TAB = SOURCE_TABLE
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
FILE_NOT_FOUND = 19
OTHERS = 22.

IF SY-SUBRC NE 0.
T_LOG-ERROR = APP_NAME.
APPEND T_LOG.
ENDIF.
ENDFORM. "DOWNLOAD_FORM

*-----------------------------------------------------------*
* FORM DOWNLOAD_DDTEXT *
*-----------------------------------------------------------*
FORM DOWNLOAD_DDTEXT USING DDTEXT_NAME.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = DDTEXT_NAME
FILETYPE = 'ASC'
TABLES
DATA_TAB = DATA_DDTEXT
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
FILE_NOT_FOUND = 19
OTHERS = 22.

IF SY-SUBRC NE 0.
T_LOG-ERROR = DDTEXT_NAME.
APPEND T_LOG.
ENDIF.
ENDFORM. "DOWNLOAD_DDTEXT

Saludos,

Blag.

3 comments:

Anonymous said...

Tuve un inconveniente con este programa, pues mi report tiene 4 includes, pero solo me reconocio uno en los archivos generados... alguna sugerencia ??

Anonymous said...

Hola tuve inconvenientes al utilizar el programa, pues solo me reconoce un include, y mi report tiene 4 include´s, alguna sugerencia ???

Alvaro "Blag" Tejada Galindo said...

Eso algo que nunca llegué a implementar...El programa funciona bien cuando se tiene la sentencia así:

INCLUDE ZTEST.
INCLUDE ZTEST1.

Pero falla cuando se hace así...

INCLUDE ZTEST. INCLUDE ZTEST1.

O así...

INCLUDE ZTEST,
ZTEST1.

La verdad es que algún dia pienso hacer la correción, pero no por el momento.

Saludos,

Blag.